I currently have an android wear watchface developed. I however would now like to create a settings section on the host app that would allow the user to customize the watchface. I am new to the android development so I am curious on the correct way to do this.
Is there a way to update a sharedpreference on the host and then push or sync that with the sharedpreference on the wear device? Or is there a totally different way I should be looking at this?
You can use the DataApi or MessageApi to sync your watchface configuration between Phone and Watch devices.
Please take a look at the documentation and choose the one more appropriate to your needs:
https://developer.android.com/training/wearables/data-layer/index.html
https://developer.android.com/training/wearables/data-layer/data-items.html
https://developer.android.com/training/wearables/data-layer/messages.html
Here is an example with the use of DataApi.
Everything pushed to the DataApi is shared between devices and available of both of them. You can change this data on both sides and the other side will be notified about such change immediately (when devices are connected to each other). You can also read this data at any moment (for example when user will choose your watchface on the Watch - the configuration data will be already waiting for you there).
On the phone side:
public class WatchfaceConfigActivity extends Activity {
private GoogleApiClient mGoogleApiClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(new ConnectionCallbacks() {
#Override
public void onConnected(Bundle connectionHint) {
}
#Override
public void onConnectionSuspended(int cause) {
}
})
.addOnConnectionFailedListener(new OnConnectionFailedListener() {
#Override
public void onConnectionFailed(ConnectionResult result) {
}
})
.addApi(Wearable.API)
.build();
mGoogleApiClient.connect();
}
and every time you want to sync new fconfiguration with the Android Wear device you have to put a DataRequest via Wearable DataApi:
private void syncConfiguration() {
if(mGoogleApiClient==null)
return;
final PutDataMapRequest putRequest = PutDataMapRequest.create("/CONFIG");
final DataMap map = putRequest.getDataMap();
map.putInt("mode", 1);
map.putInt("color", Color.RED);
map.putString("string_example", "MyWatchface");
Wearable.DataApi.putDataItem(mGoogleApiClient, putRequest.asPutDataRequest());
}
}
On the Watch side:
You need to create a class that extends WearableListenerService:
public class DataLayerListenerService extends WearableListenerService {
#Override
public void onDataChanged(DataEventBuffer dataEvents) {
super.onDataChanged(dataEvents);
final List<DataEvent> events = FreezableUtils.freezeIterable(dataEvents);
for(DataEvent event : events) {
final Uri uri = event.getDataItem().getUri();
final String path = uri!=null ? uri.getPath() : null;
if("/CONFIG".equals(path)) {
final DataMap map = DataMapItem.fromDataItem(event.getDataItem()).getDataMap();
// read your values from map:
int mode = map.getInt("mode");
int color = map.getInt("color");
String stringExample = map.getString("string_example");
}
}
}
}
and declare it in your AndroidManifest:
<service android:name=".DataLayerListenerService" >
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
Notice that this is only an example of usage. Maybe (instead of registering an instance of WearableListenerService) there will be better for you to create an instance of mGoogleApiClient inside your Watchface directly and add a DataListener there:
Wearable.DataApi.addListener(mGoogleApiClient, new DataListener() {
#Override
public void onDataChanged(DataEventBuffer dataEvents) {
// read config here and update the watchface
}
});
Maybe you don't need shared data - then you can communicate using MessageApi and send messages only when new configuration is saved or then watch wants to read current configuration from phone.
There isn't a shared preferences across the mobile and wear modules per se, but you can send messages and/or update assets that a listener will detect. For example, whenever you change a preference on the phone, you could also send a message to the watch using the Message API. On the watch, you should implement a WearableListenerService with an onMessageReceived method, in which you can parse the message and take an appropriate action, such as setting a preference on the watch.
Check out the Android Developers training guide: https://developer.android.com/training/wearables/data-layer/index.html
Related
I have a question regarding Android Wear: Wearable.DataApi.putDataItem:
My App sends some data from the mobile to the wear by using:
dataToWearRequest = PutDataRequest.create("/image");
dataToWearRequest.setData(compressedPictureAsByteArray);
Wearable.DataApi.putDataItem(mGoogleApiClient, dataToWearRequest);
The wear is receiving the data correctly by using an activity which implements DataApi.DataListener:
public void onDataChanged(DataEventBuffer dataEvents) {
for (DataEvent event : dataEvents) {
if (event.getType() == DataEvent.TYPE_CHANGED &&
event.getDataItem().getUri().getPath().equals("/image")) {
DataItem dataItem = event.getDataItem();
byte content[] = dataItem.getData();
if (mDebug) System.out.println("onDataChanged: receiver: content.length="+ content.length);
}
}
}
But my mobile app has also a WearableListenerService which is desired to receive messages from the wear. The WearableListenerService class itself also implements the DataApi.DataListener (as I said: on the mobile). In this Service the onDataChanged method also receives the data packet which was sent to the wear. It is implemented very simple (in my case because I dont need data packets from the wear):
public void onDataChanged(DataEventBuffer dataEvents) {
//super.onDataChanged(dataEvents);
for (DataEvent event : dataEvents) {
System.out.println("WEAR Data changed: "+event.getDataItem().getUri().toString());
}
dataEvents.close();
}
The corresponding manifest entry for this mobile service looks as usual:
<service android:name=".RemoteControlWearService">
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
I have tried to set an intent-filter in the manifest file of the mobile with an unknown path just to prevent it from receiving the /image data. But that did not help.
I am asking the question because the mobile indeed duplicates the data packet sent to the wear to be able to call onDataReceived. Inside the Wearable.DataApi.putDataItem seems to be a memory leak: when you send many images to the wear the memory consumption of the mobile is increasing and never garbage collected (that are native buffers). I want to reduce the memory consumption of the mobile app by preventing the sending of the unneeded parcel to the local service.
Does anybody knows how I could prevent my local mobile service from receiving the data packet directed to the wear only?
You can retrieve the local node id see here, then compare it to the one that triggered the DataEvent by calling dataItem.getUri().getHost(). The best way to compare would be by calling equals()on the value returned by dataItem.getUri().getHost() and put the localNodeId as parameter.
You can see my implementation below:
#Override
public void onDataChanged(DataEventBuffer dataEvents) {
String localNodeId = NodeUtils.getLocalNodeId(this);
for (DataEvent dataEvent : dataEvents) {
if (dataEvent.getType() == DataEvent.TYPE_CHANGED) {
DataItem dataItem = dataEvent.getDataItem();
String host = dataItem.getUri().getHost();
if (host.equals(localNodeId)) continue; // Ignore local changes
if (dataItem.getUri().getPath().equals(THE_DATA_PATH_I_WANT)) {
DataMap dataMap = DataMapItem.fromDataItem(dataItem).getDataMap();
// Do something with my DataMap
}
}
}
}
with the code used to get the node id
public class NodeUtils {
#Nullable
public static String getLocalNodeId(#NonNull Context context) {
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context)
.addApi(Wearable.API)
.build();
ConnectionResult connectionResult = googleApiClient.blockingConnect(30, TimeUnit.SECONDS);
return connectionResult.isSuccess() ? getLocalNodeId(googleApiClient) : null;
}
#Nullable
public static String getLocalNodeId(#NonNull GoogleApiClient googleApiClient) {
NodeApi.GetLocalNodeResult nodeResult =
Wearable.NodeApi.getLocalNode(googleApiClient).await(10, TimeUnit.SECONDS);
if (nodeResult.getStatus().isSuccess()) {
return nodeResult.getNode().getId();
} else {
Timber.wtf("couldn't get local node? status: %s", nodeResult.getStatus());
return null;
}
}
}
I'm following the same steps described here (the Google Fit client connection part is working fine).
final DataType dataType=TYPE_STEP_COUNT_DELTA;
DataSourcesRequest requestData = new DataSourcesRequest.Builder()
.setDataTypes(dataType) // At least one datatype must be specified.
.build();
Fitness.SensorsApi.findDataSources(mClient, requestData)
.setResultCallback(new ResultCallback<DataSourcesResult>() {
#Override
public void onResult(DataSourcesResult dataSourcesResult) {
Log.i(TAG, "Result: " + dataSourcesResult.getDataSources().size() + " sources "
+ dataSourcesResult.getStatus().toString());
for (DataSource dataSource : dataSourcesResult.getDataSources()) {
Log.i(TAG, "Data source found: " + dataSource.toString());
Log.i(TAG, "Data Source type: " + dataSource.getDataType().getName());
}
}
});
When I ask for data sources I get only one result which is the smartphone. If I add a listener then I really get data so it's working.
However it is also connected to an Android Wear smartwatch Gear Live with Android Wear app on the phone. Google Fit is installed in both of them but I'd like to get data from the smartwatch.
In the official guide I read
The Sensors API provides access to raw sensor data streams from
sensors available on the Android device and from sensors available in
companion devices, such as wearables.
This code is running on the smartphone so I think it would be right to expect data sources from companion smartwatch too. But it's like invisible to my phone application. Am I doing something wrong?
EDIT:
public class MainActivity extends AppCompatActivity {
private final static String TAG = "main_mobile";
private static final int REQUEST_OAUTH = 1;
private final static String DATE_FORMAT = "yyyy.MM.dd HH:mm:ss";
private static final String AUTH_PENDING = "auth_state_pending";
private boolean authInProgress = false;
private GoogleApiClient mClient = null;
private final static DataType dataType = TYPE_STEP_COUNT_DELTA;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
authInProgress = savedInstanceState.getBoolean(AUTH_PENDING);
}
mClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.SENSORS_API)
.addApi(Fitness.RECORDING_API)
.addApi(Fitness.HISTORY_API)
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
.addConnectionCallbacks(connectionCallbacks)
.addOnConnectionFailedListener(connectionFailCallbacks)
.build();
}
private void initFitness() {
DataSourcesRequest requestData = new DataSourcesRequest.Builder()
.setDataTypes(dataType)
.build();
Fitness.SensorsApi.findDataSources(mClient, requestData)
.setResultCallback(new ResultCallback<DataSourcesResult>() {
#Override
public void onResult(DataSourcesResult dataSourcesResult) {
Log.i(TAG, "Result: " + dataSourcesResult.getDataSources().size() + " sources " + dataSourcesResult.getStatus().toString());
for (DataSource dataSource : dataSourcesResult.getDataSources()) {
Log.i(TAG, "\nData source found: \n\t" + dataSource.toString() + "\n\tType: " + dataSource.getDataType().getName());
}
}
});
}
#Override
protected void onStart() {
super.onStart();
Log.i(TAG, "Connecting...");
mClient.connect();
}
#Override
protected void onStop() {
super.onStop();
if (mClient.isConnected()) {
mClient.disconnect();
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(AUTH_PENDING, authInProgress);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_OAUTH) {
authInProgress = false;
if (resultCode == RESULT_OK) {
// Make sure the app is not already connected or attempting to connect
if (!mClient.isConnecting() && !mClient.isConnected()) {
mClient.connect();
}
}
}
}
GoogleApiClient.ConnectionCallbacks connectionCallbacks = new GoogleApiClient.ConnectionCallbacks() {
#Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Connected!!!");
// Now you can make calls to the Fitness APIs.
// Put application specific code here.
initFitness();
}
#Override
public void onConnectionSuspended(int i) {
// If your connection to the sensor gets lost at some point,
// you'll be able to determine the reason and react to it here.
if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
Log.i(TAG, "Connection lost. Cause: Network Lost.");
} else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
Log.i(TAG, "Connection lost. Reason: Service Disconnected");
}
}
};
GoogleApiClient.OnConnectionFailedListener connectionFailCallbacks = new GoogleApiClient.OnConnectionFailedListener() {
// Called whenever the API client fails to connect.
#Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "Connection failed. Cause: " + result.toString());
if (!result.hasResolution()) {
// Show the localized error dialog
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), MainActivity.this, 0).show();
return;
}
// The failure has a resolution. Resolve it.
// Called typically when the app is not yet authorized, and an
// authorization dialog is displayed to the user.
if (!authInProgress) {
try {
Log.i(TAG, "Attempting to resolve failed connection");
authInProgress = true;
result.startResolutionForResult(MainActivity.this, REQUEST_OAUTH);
} catch (IntentSender.SendIntentException e) {
Log.e(TAG, "Exception while starting resolution activity", e);
}
}
}
};
}
I have not tried any of this.
It seems as though the Samsung Gear Live Sensors are not supported out of the box, but you might be able to make it work via software sensors:
Your Gear Live
As said in this SO answer,
The Samsung Gear Live watch does not advertise itself as a BLE heart
rate monitor and therefore does not make the heart rate data
available via the normal Bluetooth Low Energy API or the Google
Fit API which is built upon it.
Supported Sensors
As said in the official docs,
Google Fit includes support for sensors on the mobile device and
Bluetooth Low Energy sensors paired with the device. Google Fit lets
developers implement support for other sensors and expose them as
software sensors in Android apps. Sensors supported by Google Fit are
available to Android apps as data source objects.
Possible Solution
It seems possible to implement additional software sensors.
(Copied template for this is at the bottom of the post, because it is lengthy).
You would get the data on the wearable following get-heart-rate-from-sensor-samsung-gear-live.
Template (from https://developers.google.com/fit/android/new-sensors)
Add this to your manifest file:
<service android:name="com.example.MySensorService"
android:process=":sensor">
<intent-filter>
<action android:name="com.google.android.gms.fitness.service.FitnessSensorService"/>
<!-- include at least one mimeType filter for the supported data types -->
<data android:mimeType="vnd.google.fitness.data_type/com.google.heart_rate.bpm"/>
</intent-filter>
</service>
and flesh this Service out:
import com.google.android.gms.common.*;
import com.google.android.gms.common.api.*;
import com.google.android.gms.fitness.*;
import com.google.android.gms.fitness.data.*;
import com.google.android.gms.fitness.service.*;
...
public class MySensorService extends FitnessSensorService {
#Override
public void onCreate() {
super.onCreate();
// 1. Initialize your software sensor(s).
// 2. Create DataSource representations of your software sensor(s).
// 3. Initialize some data structure to keep track of a registration for each sensor.
}
#Override
protected List<DataSource> onFindDataSources(List<DataType> dataTypes) {
// 1. Find which of your software sensors provide the data types requested.
// 2. Return those as a list of DataSource objects.
}
#Override
protected boolean onRegister(FitnessSensorServiceRequest request) {
// 1. Determine which sensor to register with request.getDataSource().
// 2. If a registration for this sensor already exists, replace it with this one.
// 3. Keep (or update) a reference to the request object.
// 4. Configure your sensor according to the request parameters.
// 5. When the sensor has new data, deliver it to the platform by calling
// request.getDispatcher().publish(List<DataPoint> dataPoints)
}
#Override
protected boolean onUnregister(DataSource dataSource) {
// 1. Configure this sensor to stop delivering data to the platform
// 2. Discard the reference to the registration request object
}
}
How can I run a Xamarin.Forms.Platform.Android.FormsApplicationActivity on an Android Wear device? The call base.OnCreate(bundle) inside the onCreate method of my class always throws a RuntimeException "You cannot use indeterminate progress on a watch".
Here is my code:
namespace Test
{
[Activity (Label = "Temp.Droid", Icon = "#drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
global::Xamarin.Forms.Forms.Init (this, bundle);
LoadApplication (new App ());
}
}
}
The implementation of App should not matter since the exception gets already thrown on the call of the super onCreate and not by calling LoadApplication (new App ()) for loading the application. However its the base implementation generated by the project wizard for a Xamarin Mobile Application.
Despite the answer of James Montemagno I discovered it is possible to sync data in Xamarin Forms. I used the method of Vincent Maverick and incorporated it in Xamarin Forms. First of all take care you have the right Android SDK installed (Android Wear Tutorial - A Comprehensive Introduction).
Assuming you hve your standard app, it is advised to create Wear app in a separate Xamarin Forms Cross Platform application. This because the Wear sizes are different from Phone sizes.
In both you Wear app and your phone app right click on the References of your Android project and select MANAGE NUGET PACKAGES. Browse for wear and select
Xamarin.GooglePlayServices.Wearable Version 29.0.0 (higher versions give problems).
Click on the Properties of your Android project in both applications. Make sure the Default Namespace (Application tab) and Package name (Android Manifest tab) are the same. Also make sure the Package name does not have capitals, this will cause problems in releasing your app to the Android store.
Change the value of "Compile using Android version" to "API Level 21 (Xamarin.Android v5.0 Support).
In your Android MainActivity of both projects add usings
using Android.Gms.Wearable;
using Android.Gms.Common.Apis;
using Android.Support.V4.Content;
Then change your MainActivity of both applications to the following:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity, IDataApiDataListener, IMessageApiMessageListener
{
private static GoogleApiClient client;
const string _syncPath = "/MySyncPath/Data";
static string device = "Watch";
static string text= "";
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
client = new GoogleApiClient.Builder(this)
.AddApi(WearableClass.API)
.Build();
IntentFilter filter = new IntentFilter(Intent.ActionSend);
MessageReciever receiver = new MessageReciever(this);
LocalBroadcastManager.GetInstance(this).RegisterReceiver(receiver, filter);
}
internal class MessageReciever : BroadcastReceiver
{
MainActivity _main;
public MessageReciever(MainActivity owner) { this._main = owner; }
public override void OnReceive(Context context, Intent intent)
{
_main.ProcessMessage(intent);
}
}
public void OnDataChanged(DataEventBuffer dataEvents)
{
var dataEvent = Enumerable.Range(0, dataEvents.Count)
.Select(i => dataEvents.Get(i).JavaCast<IDataEvent>())
.FirstOrDefault(x => x.Type == DataEvent.TypeChanged && x.DataItem.Uri.Path.Equals(_syncPath));
if (dataEvent == null)
return;
//do stuffs here
}
public override void OnBackPressed()
{
base.OnBackPressed();
}
protected override void OnStart()
{
base.OnStart();
Android.Util.Log.Info("WearIntegration", "Received Message");
client.Connect();
}
public void OnConnected(Bundle p0)
{
WearableClass.DataApi.AddListener(client, this);
}
public void OnConnectionSuspended(int reason)
{
Android.Util.Log.Error("GMSonnection suspended " + reason, "");
WearableClass.DataApi.RemoveListener(client, this);
}
public void OnConnectionFailed(Android.Gms.Common.ConnectionResult result)
{
Android.Util.Log.Error("GMSonnection failed " + result.ErrorCode, "");
}
protected override void OnStop()
{
base.OnStop();
client.Disconnect();
}
public void OnMessageReceived(IMessageEvent messageEvent)
{
if (messageEvent.Path.Equals(_syncPath))
{
var msg = System.Text.Encoding.UTF8.GetString(messageEvent.GetData());
this.RunOnUiThread(() =>
Android.Widget.Toast.MakeText(this, msg, ToastLength.Long).Show());
}
}
public void ProcessMessage(Intent intent)
{
if (intent.GetStringExtra("Device") != device)
{
text = intent.GetStringExtra("WearMessage");
//do stuffs here
}
}
public void SendData() {
try {
var request = PutDataMapRequest.Create(_syncPath);
var map = request.DataMap;
map.PutString("Device", device);
map.PutString("Message", "Xamarin Forms says Hello from Wearable!");
map.PutLong("UpdatedAt", DateTime.UtcNow.Ticks);
WearableClass.DataApi.PutDataItem(_client, request.AsPutDataRequest());
}
finally {
_client.Disconnect();
}
}
In your Phone application change the static string device to Phone and change the message text if you want to:
static string device = "Phone";
map.PutString("Message", "Xamarin Forms says Hello from Phone!");
Then add the WearService class to both your Android Projects add the same usings as added to the MAinActivity an change the Wearservice as follows:
[Service]
[IntentFilter(new[] { "com.google.android.gms.wearable.BIND_LISTENER" })]
public class WearService : WearableListenerService
{
const string _syncPath = "/KorfballTimer/Data";
GoogleApiClient _client;
public override void OnCreate()
{
base.OnCreate();
_client = new GoogleApiClient.Builder(this.ApplicationContext)
.AddApi(WearableClass.API)
.Build();
_client.Connect();
Android.Util.Log.Info("WearIntegrationreated", "");
}
public override void OnDataChanged(DataEventBuffer dataEvents)
{
var dataEvent = Enumerable.Range(0, dataEvents.Count)
.Select(i => dataEvents.Get(i).JavaCast<IDataEvent)
.FirstOrDefault(x => x.Type == DataEvent.TypeChanged && x.DataItem.Uri.Path.Equals(_syncPath));
if (dataEvent == null)
return;
//get data from wearable
var dataMapItem = DataMapItem.FromDataItem(dataEvent.DataItem);
var map = dataMapItem.DataMap;
string message = dataMapItem.DataMap.GetString("Message");
Intent intent = new Intent();
intent.SetAction(Intent.ActionSend);
intent.PutExtra("WearMessage", message);
intent.PutExtra("Device", map.GetString("Device"));
LocalBroadcastManager.GetInstance(this).SendBroadcast(intent);
}
}
And finally, Add the meta data in the AndroidManifest.xml under element:
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
If you don't want the IOS and Windows projects in your Wear application, just delete them. Now you can build your Wear application in Xamarin Forms just as you do with your phone application. Happy Coding.
You would not run a Xamarin.Forms application on a wearable device. You would need to create a new Android Wear application in native Xamarin.Android. Wearable Applications use a special theme, special controls, and have special APIs. A good sample to look at is how I did Hanselman.Forms, which is a Xamarin.Forms main application but ties in an Android Wear application as well: https://github.com/jamesmontemagno/Hanselman.Forms
I'm pretty new with Android programming. But I have been working on this for over a week now, and it starts to get booooring.
My idea is that I want to connect two devices using Wifi Direct. But I only want to connect to those which are running my application. Besides, I want the users to be able to see some information of the other devices (such as user name), not just the MAC or the Android_XXXX name included in the WifiP2pDevice. That's why I decided that a device looking for other devices, should both start the application service and search for peers which are also broadcasting this service.
The problem (I'm testing with two real devices) is that, even though they are running exactly the same code, only one of them is getting the service discovery callbacks (the onDnsSd... listeners below). So, one side acts in the proper way, but not the other. Moreover I'm getting "old" services, meaning that apparently each time I start de service (even though I cancel previously started services), that service seems to be still broadcast during at least some minutes.
I include a shortened version of my code:
public class MoveFlufietsDialogFragment extends DialogFragment implements ChannelListener, DeviceActionListener {
public final HashMap<String, FlufietsPeer> mBuddies = new HashMap<String, FlufietsPeer>();
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
...
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
mManager = (WifiP2pManager) getActivity().getSystemService(Context.WIFI_P2P_SERVICE);
mChannel = mManager.initialize(getActivity(), getActivity().getMainLooper(), null);
...
startRegistration();
discoverFlufietsService();
...
}
public void discoverFlufietsService() {
DnsSdTxtRecordListener txtListener = new DnsSdTxtRecordListener() {
#Override
public void onDnsSdTxtRecordAvailable(String fullDomain, Map record, WifiP2pDevice device) {
// This and the next listener are only called in one of the devices.
String serviceName = (String) record.get("serviceName");
if ((serviceName != null) && (serviceName.equals("flufiets")) {
// I put the record data in the mBuddies HashMap.
...
mBuddies.put(device.deviceAddress, myPeerDataStructure);
}
}
};
DnsSdServiceResponseListener servListener = new DnsSdServiceResponseListener() {
#Override
public void onDnsSdServiceAvailable(String instanceName, String registrationType, WifiP2pDevice resourceType) {
if (mBuddies.containsKey(resourceType.deviceAddress)) {
FlufietsPeer flufietsPeer = mBuddies.get(resourceType.deviceAddress);
WiFiPeerListAdapter adapter = ((WiFiPeerListAdapter) mFragmentList.getListAdapter());
adapter.add(flufietsPeer);
adapter.notifyDataSetChanged();
}
}
};
mManager.setDnsSdResponseListeners(mChannel, servListener, txtListener);
WifiP2pDnsSdServiceRequest serviceRequest = WifiP2pDnsSdServiceRequest.newInstance();
mManager.addServiceRequest(mChannel, serviceRequest, new ActionListener() {
// onSuccess/onFailure toasts.
});
mManager.discoverServices(mChannel, new WifiP2pManager.ActionListener() {
// onSuccess/onFailure toasts.
});
}
public void startRegistration() {
mManager.clearLocalServices(mChannel, new ActionListener() {
// onSuccess/onFailure toasts.
});
Map record = new HashMap();
record.put("serviceName", "flufiets");
...
WifiP2pDnsSdServiceInfo serviceInfo = WifiP2pDnsSdServiceInfo.newInstance(flufietsService, "_tcp", record);
mManager.addLocalService(mChannel, serviceInfo, new ActionListener() {
// onSuccess/onFailure toasts.
});
}
#Override
public void onResume() {
super.onResume();
mReceiver = new WiFiDirectBroadcastReceiver(mManager, mChannel, this);
getActivity().registerReceiver(mReceiver, mIntentFilter);
}
#Override
public void onPause() {
super.onPause();
getActivity().unregisterReceiver(mReceiver);
}
#Override
public void onStop() {
super.onStop();
mManager.clearLocalServices(mChannel, new ActionListener() {
// onSuccess/onFailure toasts.
});
}
...
}
The problem doesn't seem to be related with the device itself (sometimes it works, sometimes it doesn't, but always only in one of them). I suspect it has to do with either trying to discover a service that we ourselves are broadcasting, or having the same service being offered by two devices. I have tried changing the names of the service, so each device would offer either a "send" or "receive" service, but it doesn't work. I only get the callbacks called (onDnsSd...) in one of the devices.
And that thing about getting old services, when I always clear them, is weird (I do include a timestamp in the service record data, and I could always discard all but the last, but doesn't seem to be logical).
Any ideas? ANY help would be VERY appreciated, because writing the application is not funny any more (:-)=
Thanks a lot!
You need to wait until the clearLocalService call succeeds before adding the local service later. So put the addLocalService call into the onSuccess callback of the clearLocalServices.
I know that Wifi Direct works by creating a Soft AP (software access point) in one of the devices. I also know that many Androids support Wifi Direct, but iPhones do not.
My question is: is it possible to create a device-to-device wifi link that is Wifi Direct on the Android side, but regular wifi on the iPhone side? Where the Android's Wifi Direct would be presenting a soft AP, which the iPhone would see as indistinguishable from a regular AP and be able to associate to.
Imagine that this is out in the wilderness where no router AP is available. Also, neither user has a tethering plan.
This link would be used by a Bump-like app to transfer files.
Depending on your phone you can just set up your Android phone as a portable hotspot and connect to that with the iPhone. From there it would be application specific to get data transferred.
However you can also use the Androids WiFi-Direct libraries. In that case you would use them to set up the Android phone to create a "Group owner", which basically is the same as it being a portable hotspot. Check out:
http://developer.android.com/guide/topics/connectivity/wifip2p.html
I'll give you a code example to help you get started.
public class WifiDirectAPtestActivity extends Activity
{
private WifiP2pManager manager;
private boolean isWifiP2pEnabled = false;
private boolean retryChannel = false;
private final IntentFilter intentFilter = new IntentFilter();
private Channel channel;
private BroadcastReceiver receiver = null;
public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled) {
this.isWifiP2pEnabled = isWifiP2pEnabled;
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// add necessary intent values to be matched.
intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
channel = manager.initialize(this, getMainLooper(), null);
}
/** register the BroadcastReceiver with the intent values to be matched */
#Override
public void onResume() {
super.onResume();
receiver = new WiFiDirectBroadcastReceiver(manager, channel, this);
registerReceiver(receiver, intentFilter);
createGroup();
}
#Override
public void onPause() {
super.onPause();
unregisterReceiver(receiver);
manager.removeGroup(channel, new ActionListener() {
#Override
public void onFailure(int reasonCode) {
Log.d("WifiDirectAptestActivity", "Disconnect failed. Reason :" + reasonCode);
}
#Override
public void onSuccess() {
Log.d("WifiDirectAptestActivity", "Should have been sucessfully removed");
}
});
}
public void createGroup()
{
manager.createGroup(channel, new ActionListener() {
#Override
public void onSuccess() {
// WiFiDirectBroadcastReceiver will notify us. Ignore for now.
Log.d("WifiDirectAPtestActivity", "Group creating request successfully send");
}
#Override
public void onFailure(int reason) {
Toast.makeText(WifiDirectAPtestActivity.this, "Connect failed. Retry.",
Toast.LENGTH_SHORT).show();
}
});
}
In addition you'll need the broadcast receiver, look at the WiFi-Direct demo and it should be clear to you.
Note that line manager.createGroup(channel, new ActionListener() is the codeline of interest, it is this line that actually sets up the device as a portable hotspot.
Hope this clarifies things, I don't really know how detailed explanation you need. Comment if some things are not clear.