Related
So I've made this app, where I find all BLE Devices with a name. But how can I make one of the specific fields, clickable and automatic connect to the device, so I can start writing/reading from it?
Adapter
public class ListAdapter_BTLE_Devices extends ArrayAdapter<BTLE_Device> {
Activity activity;
int layoutResourceID;
ArrayList<BTLE_Device> devices;
public ListAdapter_BTLE_Devices(Activity activity, int resource, ArrayList<BTLE_Device> objects) {
super(activity.getApplicationContext(), resource, objects);
this.activity = activity;
layoutResourceID = resource;
devices = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater =
(LayoutInflater) activity.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(layoutResourceID, parent, false);
}
BTLE_Device device = devices.get(position);
String name = device.getName();
String address = device.getAddress();
int rssi = device.getRSSI();
TextView BLE_name = (TextView) convertView.findViewById(R.id.BLE_name);
if (name != null && name.length() > 0) {
BLE_name.setText(device.getName());
}
else {
BLE_name.setText("No Name");
}
TextView BLE_rssi = (TextView) convertView.findViewById(R.id.BLE_rssi);
BLE_rssi.setText("RSSI: " + Integer.toString(rssi));
TextView BLE_macaddr = (TextView) convertView.findViewById(R.id.BLE_macaddr);
if (address != null && address.length() > 0) {
BLE_macaddr.setText("MAC-addr: "+device.getAddress());
}
else {
BLE_macaddr.setText("No Address");
}
return convertView;
}
}
EDIT
I think i might be connected to the GATT now, so what I've done is..
To start with i get the MAC-addr from the Mainactivity and then I saved it in a intent, and started another activity onclick.
Here I did the follwing
DeviceAddress = intent.getStringExtra(MainActivity.EXTRAS_BLE_ADDRESS);
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(DeviceAddress);
device.connectGatt(this, false, mGattCallback);
and when I call connectGatt it prints the message Log.d(TAG, "Connection State: 1");, is this the right way to do it?
private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
#Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
Log.d(TAG, "Connection State Change: "+status+" -> "+connectionState(newState));
if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_CONNECTED) {
/*
* Once successfully connected, we must next discover all the services on the
* device before we can read and write their characteristics.
*/
Log.d(TAG, "Connection State: 1");
gatt.discoverServices();
} else if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_DISCONNECTED) {
/*
* If at any point we disconnect, send a message to clear the weather values
* out of the UI
*/
Log.d(TAG, "Connection State: 2");
} else if (status != BluetoothGatt.GATT_SUCCESS) {
/*
* If there is a failure at any stage, simply disconnect
*/
Log.d(TAG, "Connection State: 3");
gatt.disconnect();
}
}
If you have problems with Bluetooth LE I suggest you to use my bluetooth le library (don't reinvent the wheel, it tooks me about 3/4 months to make the library, a bluetooth le communication can be really tricky to make), it is open source so you can also see the code for having an example of implementation, I link you the github page: https://github.com/niedev/BluetoothCommunicator
For use the library in a project you have to add jitpack.io to your root build.gradle (project):
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Then add the last version of BluetoothCommunicator to your app build.gradle
dependencies {
implementation 'com.github.niedev:BluetoothCommunicator:1.0.6'
}
To use this library add these permissions to your manifest:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Then add android:largeHeap="true" to the application tag in the manifest:
Example
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:name="com.bluetooth.communicatorexample.Global"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:largeHeap="true"
android:theme="#style/Theme.Speech">
<activity android:name="com.bluetooth.communicatorexample.MainActivity"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Once you have downloaded the libray and set the manifest, you need to create a bluetooth communicator object, it is the object that handles all operations of bluetooth low energy library, if you want to manage the bluetooth connections in multiple activities I suggest you to save this object as an attribute of a custom class that extends Application and create a getter so you can access to bluetoothCommunicator from any activity or service with:
((custom class name) getApplication()).getBluetoothCommunicator();
Next step is to initialize bluetoothCommunicator, the parameters are: a context, the name by which the other devices will see us (limited to 18 characters and can be only characters listed in BluetoothTools.getSupportedUTFCharacters(context) because the number of bytes for advertising beacon is limited) and the strategy (for now the only supported stategy is BluetoothCommunicator.STRATEGY_P2P_WITH_RECONNECTION)
bluetoothCommunicator = new BluetoothCommunicator(this, "device name", BluetoothCommunicator.STRATEGY_P2P_WITH_RECONNECTION);
Then add the bluetooth communicator callback, the callback will listen for all events of bluetooth communicator:
bluetoothCommunicator.addCallback(new BluetoothCommunicator.Callback() {
#Override
public void onBluetoothLeNotSupported() {
super.onBluetoothLeNotSupported();
Notify that bluetooth low energy is not compatible with this device
}
#Override
public void onAdvertiseStarted() {
super.onAdvertiseStarted();
Notify that advertise has started, if you want to do something after the start of advertising do it here, because
after startAdvertise there is no guarantee that advertise is really started (it is delayed)
}
#Override
public void onDiscoveryStarted() {
super.onDiscoveryStarted();
Notify that discovery has started, if you want to do something after the start of discovery do it here, because
after startDiscovery there is no guarantee that discovery is really started (it is delayed)
}
#Override
public void onAdvertiseStopped() {
super.onAdvertiseStopped();
Notify that advertise has stopped, if you want to do something after the stop of advertising do it here, because
after stopAdvertising there is no guarantee that advertise is really stopped (it is delayed)
}
#Override
public void onDiscoveryStopped() {
super.onDiscoveryStopped();
Notify that discovery has stopped, if you want to do something after the stop of discovery do it here, because
after stopDiscovery there is no guarantee that discovery is really stopped (it is delayed)
}
#Override
public void onPeerFound(Peer peer) {
super.onPeerFound(peer);
Here for example you can save peer in a list or anywhere you want and when the user
choose a peer you can call bluetoothCommunicator.connect(peer founded) but if you want to
use a peer for connect you have to have peer updated (see onPeerUpdated or onPeerLost), if you use a
non updated peer the connection might fail
instead if you want to immediate connect where peer is found you can call bluetoothCommunicator.connect(peer) here
}
#Override
public void onPeerLost(Peer peer){
super.onPeerLost(peer);
It means that a peer is out of range or has interrupted the advertise,
here you can delete the peer lost from a eventual collection of founded peers
}
#Override
public void onPeerUpdated(Peer peer,Peer newPeer){
super.onPeerUpdated(peer,newPeer);
It means that a founded peer (or connected peer) has changed (name or address or other things),
if you have a collection of founded peers, you need to replace peer with newPeer if you want to connect successfully to that peer.
In case the peer updated is connected and you have saved connected peers you have to update the peer if you want to successfully
send a message or a disconnection request to that peer.
}
#Override
public void onConnectionRequest(Peer peer){
super.onConnectionRequest(peer);
It means you have received a connection request from another device (peer) (that have called connect)
for accept the connection request and start connection call bluetoothCommunicator.acceptConnection(peer);
for refusing call bluetoothCommunicator.rejectConnection(peer); (the peer must be the peer argument of onConnectionRequest)
}
#Override
public void onConnectionSuccess(Peer peer,int source){
super.onConnectionSuccess(peer,source);
This means that you have accepted the connection request using acceptConnection or the other
device has accepted your connection request and the connection is complete, from now on you
can send messages or data (or disconnection request) to this peer until onDisconnected
To send messages to all connected peers you need to create a message with a context, a header, represented by a single character string
(you can use a header to distinguish between different types of messages, or you can ignore it and use a random
character), the text of the message, or a series of bytes if you want to send any kind of data and the peer you want to send the message to
(must be connected to avoid errors), example: new Message(context,"a","hello world",peer);
If you want to send message to a specific peer you have to set the sender of the message with the corresponding peer.
To send disconnection request to connected peer you need to call bluetoothCommunicator.disconnect(peer);
}
#Override
public void onConnectionFailed(Peer peer,int errorCode){
super.onConnectionFailed(peer,errorCode);
This means that your connection request is rejected or has other problems,
to know the cause of the failure see errorCode (BluetoothCommunicator.CONNECTION_REJECTED
means rejected connection and BluetoothCommunicator.ERROR means generic error)
}
#Override
public void onConnectionLost(Peer peer){
super.onConnectionLost(peer);
This means that a connected peer has lost the connection with you and the library is trying
to restore it, in this case you can update the gui to notify this problem.
You can still send messages in this situation, all sent messages are put in a queue
and sent as soon as the connection is restored
}
#Override
public void onConnectionResumed(Peer peer){
super.onConnectionResumed(peer);
Means that connection lost is resumed successfully
}
#Override
public void onMessageReceived(Message message,int source){
super.onMessageReceived(message,source);
Means that you have received a message containing TEXT, for know the sender you can call message.getSender() that return
the peer that have sent the message, you can ignore source, it indicate only if you have received the message
as client or as server
}
#Override
public void onDataReceived(Message data,int source){
super.onDataReceived(data,source);
Means that you have received a message containing DATA, for know the sender you can call message.getSender() that return
the peer that have sent the message, you can ignore source, it indicate only if you have received the message
as client or as server
}
#Override
public void onDisconnected(Peer peer,int peersLeft){
super.onDisconnected(peer,peersLeft);
Means that the peer is disconnected, peersLeft indicate the number of connected peers remained
}
#Override
public void onDisconnectionFailed(){
super.onDisconnectionFailed();
Means that a disconnection is failed, super.onDisconnectionFailed will reactivate bluetooth for forcing disconnection
(however the disconnection will be notified in onDisconnection)
}
});
Finally you can start discovery and/or advertising:
bluetoothCommunicator.startAdvertising();
bluetoothCommunicator.startDiscovery();
All other actions that can be done are explained with the comments in the code of callback I wrote before.
To connect to the Device first you must perform you BLE scan which (if your using the starter code) runs a callback and add it to a list of found devices.
Add a filter in to only allow the set device you are looking for. As BLE advertises a packet upto 31 bytes you should have some data in here which discerns you device such as manufacturer id or data etc. Or if you are working on a simple project you can programmatically hard code in the device address.
Then when this device is discovered from the scan you can stop your BLE scan and automatically queue a connection request. This will ask for the GATT request to be made and therefore, grant you access to the GATT services and thus characteristics on the device.
You can add a view to your holder and set a click listener to it. A view could be a transparent rectangle all around your display card (or whatever you use).
I'd suggest this in depth read regarding BLE usage. On the click listener you can queue up the connection request.
I have Android application which exposes BLE Server. I connect with BluetoothGattServer#connect. It works - my app gets call to BluetoothGattServerCallback#onConnectionStateChange with STATE_CONNECTED. When I'm done with the client I try to disconnect from my app with BluetoothGattServer#cancelConnection.
But I do not get call to BluetoothGattServerCallback#onConnectionStateChange and it seems that the connection is still active as my BLE client does not start to advertise (which it does when nothing is connected to it).
In logcat I see only:
BluetoothGattServer: cancelConnection() - device: XX:XX:XX:XX:XX:XX
The funny part is, my app gets call to BluetoothGattServerCallback#onConnectionStateChange with STATE_DISCONNECTED as soon as I turn off BT completely.
Similar issues in Google's tracker: 63461 and 63464.
When newState==BluetoothProfile.STATE_CONNECTED, you have to call BluetoothGattServer.connect();.
#Override
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
super.onConnectionStateChange(device, status, newState);
if (newState == BluetoothProfile.STATE_CONNECTED){
mDevice = device;
mBluetoothGattServer.connect(device, false);
}else {
mDevice = null;
}
}
private void cancelConnection(){
if (mDevice != null) {
mBluetoothGattServer.cancelConnection(mDevice);
}
}
Encountering same issue when calling disconnect() method.. no disconnect is given in onConnectionStateChange in my BluetoothGattCallback.
Cycling Bluetooth seems the be the only thing that works.
edit:
also, after disconnect() and close() method are called, I am still connected according to this code:
public int getConnectedBLEDevices() {
int i = 0;
List<BluetoothDevice> devices = mBluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
for(BluetoothDevice device : devices) {
if(device.getType() == BluetoothDevice.DEVICE_TYPE_LE) {
Logs.writeEvent(TAG+".getConnectedBLEDevices()", device.getAddress() + "\n"+ getStateAsString(mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT)));
i++;
}
}
return i;
}
pls see https://issuetracker.google.com/issues/37127644
Status: Won't Fix (Intended Behavior)
You must call BluetoothGattServer.connect() to mark connection as used, then BluetoothGattServer.disconnect() to mark it as no longer used. Then after a timeout stack can decide to disconnect from the remote if no one else is using the connection.
If BluetoothGattServer.connect() is not called after the connection is established, then the stack is keeping the connection until some gatt client/server app start using this connection.
I am developing an Android Application that connects to a BLE Device and reads the Gatt Services and Gatt Characteristics. I used the BluetoothLeGatt sample project from the Android Development Site as my reference.
So far, I am able to programmatically connect to a device (I took note of my Device's Address to be able to do this) and filter out the specific Gatt Service I want to read and that Services' specific Characteristics by taking note of the UUID of both the Service and the Characteristics. The sample provided by Google also updates whenever there's a message sent from my BLE Device to my Android Application. Overall, I have no problems at this end.
However, upon reading up further on GATT, I found that it is possible to connect to multiple BLE devices (all slaves OR servers - being the ones that send the data) using a single Android Application (as master OR client - as the one who receives said data). So what I tried to do was to have 2 BLE Devices (different Address), took note of their Address, and then my application tries to connect to them once the application sees that those 2 addresses are up and running.
In code, I call this function when I see my 2 BLE Devices:
private void connectToDevice(){
mDeviceName = deviceList.get(currentIndex).getName();
mDeviceAddress = deviceList.get(currentIndex).getAddress();
Log.e(TAG, "connecting to device name = " + mDeviceName);
mBluetoothLeService.connect(mDeviceAddress);
}
Where currentIndex is initially set to zero. Then once I get a successful connection, I do:
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
Log.e(TAG, "connected");
mConnected = true;
if(currentIndex < deviceList.size()-1) currentIndex ++;
connectToDevice();
}
}
};
Where I check if I still have devices to connect to in my deviceList, if so, increment my counter and then connect until I exhaust everything in my list.
However, I seem to have no success at all using this method.
Kindly note that switching connection (round robin) between my devices isn't an option. This will be an issue when I have a lot of devices and it's important to get their messages real time without delays. This said, I have to have a live connection to my devices.
Has anyone tried to connect to multiple BLE Devices in Android? I'm not sure on how to proceed on this.
Indeed it is possible to connect to more than one peripheral from your Android device. However, it will make your code much more complex since you will need to manage each connection and responses.
For each connection you would have to implement a BluetoothGatt with it's callbacks. I tested it many months ago with a dummy test and as I said, it worked well and I was able to connect to different peripherals. However, if you chain many commands there seem to be some overlapping issues described in this thread.
As asked here is the relevant code : (Here the ArrayList contains the founded peripheral devices)
for(int i=0;i< Utility.selectedDeviceList.size();i++) {
Log.d(Utility.TAG,"state"+ Utility.selectedDeviceList.get(i).getmConnectionState());
if (Utility.selectedDeviceList.get(i).getmConnectionState() != Utility.CONNECTED) {
Log.d(Utility.TAG,"Connecting LeSerive::" + Utility.selectedDeviceList.get(i).getAddress());
Utility.mBluetoothLeService.connect(i, Utility.selectedDeviceList.get(i).getAddress());
}
}
This for loop is a part of runnable interface which is called inside a handler having a looper.
public void run() {
Looper.prepare();
Looper mLooper = Looper.myLooper();
Log.d(Utility.TAG,"BLE Thread Started::");
mHandler = new Handler(mLooper) {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case Utility.BLE_SYNC:
Log.d(Utility.TAG,"BLE Sync Connecting::");
mHandler.post(SynState);
break;
}
};
Looper.loop();
}
I used this approach because their is lot of communication between peripherals to send and receive the data from them.
This is the connect method which inside a Service :
public boolean connect(int tag,final String address) {
if (mBluetoothAdapter == null || address == null) {
Log.w(Utility.TAG, "BluetoothAdapter not initialized or unspecified address.");
return false;
}
Utility.selectedDeviceList.get(tag).setmConnectionState(Utility.CONNECTING);
if( Utility.selectedDeviceList.get(tag).getmBluetoothGatt()==null){
Log.w(Utility.TAG, "new connect :: "+ Utility.selectedDeviceList.get(tag).getAddress());
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
if (device == null) {
Log.w(Utility.TAG, "Device not found. Unable to connect.");
return false;
}
try {
Utility.selectedDeviceList.get(tag).setmBluetoothGatt(device.connectGatt(this, false, mGattCallback));
}
catch (Exception e)
{
e.printStackTrace();
Log.d(Utility.TAG,"ConnectGatt exception caught");
}
}
return true;
}
This is the mGattCallBack :
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
#Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
}
#Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.d(Utility.TAG, "onServicesDiscovered");
}
#Override
public void onCharacteristicRead(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic,int status) {
}
#Override
public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
Log.d(Utility.TAG,">>onCharacteristicWrite");
}
#Override
public void onCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic) {
}
};
Hope it clears few things for you
It is possible to connect to multiple devices at a time. in my experience it works pretty stable and the number of devices you can connect to (stable) depends on your hardware. I found out that best practise (for me) was to create one separate service for the scanning stuff and one service for each Bluetoothconnection. it's important not to use bound services because the termination of a connection is not stible when binding to it.
With this pattern you can control your connection easily. To transport data out of your service you can use a broadcastreceiver, for example if you want to display the data in your main activity. Termination of the connection is pretty important so stop the service and in onDestroy call
mConnectedGatt.disconnect();
ble_device=null;
For the Scanning part I've used a List of Strings where I saved all the mac Adresses I want to find. When i found one device I deleted it from the list and if the list is empty it stopped the scanner service. To transmit my found device I used a broadcastreceiver and sent it to my main Activity. There I transmitted it to the right service.
Hope this helps
Wifi P2P service discovery is not behaving as expected. I am seeing intermittent issues where the DNSSD listeners are not called always and hence I have no clue of nearby devices running the same app. I am using the following two APIs - one to register a service to be discovered by other devices and the other to discover the nearby services running on other devices. Any idea if I am doing anything wrong here or is there some specific sequence of other android API calls that need to be made before I call these APIs to ensure that the listeners are always called whenever there is a new service registered or even if a service is registered before we call the API to discover the local services.
API to register a local service:
private void registerService() {
Map<String, String> values = new HashMap<String, String>();
values.put("name", "Steve");
values.put("port", "8080");
WifiP2pServiceInfo srvcInfo = WifiP2pDnsSdServiceInfo.newInstance(mMyDevice.deviceName, "_http._tcp", values);
manager.addLocalService(channel, srvcInfo, new WifiP2pManager.ActionListener() {
#Override
public void onSuccess() {
Toast.makeText(WiFiDirectActivity.this, "Local service added successfully",
Toast.LENGTH_SHORT).show();
}
#Override
public void onFailure(int reasonCode) {
Toast.makeText(WiFiDirectActivity.this, "Local service addition failed : " + reasonCode,
Toast.LENGTH_SHORT).show();
}
});
}
API to discover local services:
public void discoverService() {
manager.clearServiceRequests(channel, null);
DnsSdTxtRecordListener txtListener = new DnsSdTxtRecordListener() {
#Override
/* Callback includes:
* fullDomain: full domain name: e.g "printer._ipp._tcp.local."
* record: TXT record data as a map of key/value pairs.
* device: The device running the advertised service.
*/
public void onDnsSdTxtRecordAvailable(String fullDomain, Map record, WifiP2pDevice device) {
Log.d(TAG, "DnsSdTxtRecord available -" + record.toString());
}
};
DnsSdServiceResponseListener servListener = new DnsSdServiceResponseListener() {
#Override
public void onDnsSdServiceAvailable(String instanceName, String registrationType, WifiP2pDevice resourceType) {
Log.d(TAG, "onBonjourServiceAvailable " + instanceName);
}
};
manager.setDnsSdResponseListeners(channel, servListener, txtListener);
WifiP2pDnsSdServiceRequest serviceRequest = WifiP2pDnsSdServiceRequest.newInstance();
manager.addServiceRequest(channel, serviceRequest, new ActionListener() {
#Override
public void onSuccess() {
// Success!
Log.d(TAG, "addServiceRequest success");
}
#Override
public void onFailure(int code) {
// Command failed. Check for P2P_UNSUPPORTED, ERROR, or BUSY
Log.d(TAG, "addServiceRequest failure with code " + code);
}
});
manager.discoverServices(channel, new ActionListener() {
#Override
public void onSuccess() {
// Success!
Log.d(TAG, "discoverServices success");
}
#Override
public void onFailure(int code) {
// Command failed. Check for P2P_UNSUPPORTED, ERROR, or BUSY
if (code == WifiP2pManager.P2P_UNSUPPORTED) {
Log.d(TAG, "P2P isn't supported on this device.");
} else {
Log.d(TAG, "discoverServices failure");
}
}
});
}
Note: manager & channel are initialized as
WifiP2pManager manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
Channel channel = manager.initialize(this, getMainLooper(), null);
WifiP2p (in general):
Some time ago I was developing an application with a pretty complex network connectivity system based on WifiP2p with Service Broadcasting/Discovery. And based on that experience I already wrote few posts here on SO about how difficult, wearing and problematic that is. Here are two of them (they are quite full of the inside knowledge I acquired about WifiP2p with Service Discovery, and WifiP2p itself):
Why is discovering peers for Android WifiDirect so unreliable
Wi-fi P2P. Inform all peers available of some event
I would advise you to read both of my answers (even though they are focused a bit more on the WifiP2p itself). They should give you some perspective on the things you should be looking for when working with the WifiP2p Service Discovery.
I can easily say that if you want to build an efficient, relatively reliable and robust WifiP2p connection system (especially with Service Discovery), you will have to work your ass off.
WifiP2p Service Discovery:
To better answer your exact question, I will tell you what I did (different from you) to make my Service Discovery work pretty reliably.
1. Broadcasting Service:
First of all: before registering your Service (with addLocalService method) you should use the WifiP2pManager's clearLocalServices method. And it is important, that you should only call addLocalService if the listener passed in the clearLocalServices returned with the onSuccess callback.
Although this sets up the broadcasting pretty nicely, I found that other nodes were not always able to detect the broadcasted service (especially when those nodes weren't already actively detecting services at the moment of registering your local Service - but they "joined" later). I couldn't find a way to fix this issue 100% reliably. And believe me I was trying probably everything WifiP2p-related. And no, the clearLocalServices-addLocalService sequence wasn't really giving satisfying results. Or more so: doing something different was working much better. What I decided to do, was after I successfully added local service (onSuccess callback from addLocalService), I started a Thread that would periodically call WifiP2pManager's method discoverPeers. That seemed to be forcing to rebroadcast all the service information.
So... basically the base of your broadcasting code should look more-less like this (bare in mind that every single piece of code I will post here is stripped-off of all "checks" if the network connectivity system is in the right state, you should design them yourself to fit your solution the best):
public void startBroadcastingService(){
mWifiP2pManager.clearLocalServices(mWifiP2pChannel, new WifiP2pManager.ActionListener() {
#Override
public void onSuccess() {
mWifiP2pManager.addLocalService(mWifiP2pChannel, mWifiP2pServiceInfo,
new WifiP2pManager.ActionListener() {
#Override
public void onSuccess() {
// service broadcasting started
mServiceBroadcastingHandler
.postDelayed(mServiceBroadcastingRunnable,
SERVICE_BROADCASTING_INTERVAL);
}
#Override
public void onFailure(int error) {
// react to failure of adding the local service
}
});
}
#Override
public void onFailure(int error) {
// react to failure of clearing the local services
}
});
}
where the mServiceBroadcastingRunnable should be:
private Runnable mServiceBroadcastingRunnable = new Runnable() {
#Override
public void run() {
mWifiP2pManager.discoverPeers(mWifiP2pChannel, new WifiP2pManager.ActionListener() {
#Override
public void onSuccess() {
}
#Override
public void onFailure(int error) {
}
});
mServiceBroadcastingHandler
.postDelayed(mServiceBroadcastingRunnable, SERVICE_BROADCASTING_INTERVAL);
}
};
2. Discovering Service:
For the discovering of your service I used similar approach. Both with the setting up the discovering, and with trying to force "rediscovery" of services.
Setting up was performed with the sequence of the following three WifiP2pManager's methods:
removeServiceRequest, addServiceRequest, discoverServices
They were called in this exact order and a particular method (second or the third one to be exact) has been called only after the previous one had "returned" with the onSuccess callback.
The rediscovery of services was being performed with the intuitive method (just by repeating the mentioned sequence: removeServiceRequest -> addServiceRequest -> discoverServices).
The base of my code looked more-less like this (to start Service Discovery I would first call prepareServiceDiscovery() and then startServiceDiscovery()):
public void prepareServiceDiscovery() {
mWifiP2pManager.setDnsSdResponseListeners(mWifiP2pChannel,
new WifiP2pManager.DnsSdServiceResponseListener() {
#Override
public void onDnsSdServiceAvailable(String instanceName,
String registrationType, WifiP2pDevice srcDevice) {
// do all the things you need to do with detected service
}
}, new WifiP2pManager.DnsSdTxtRecordListener() {
#Override
public void onDnsSdTxtRecordAvailable(
String fullDomainName, Map<String, String> record,
WifiP2pDevice device) {
// do all the things you need to do with detailed information about detected service
}
});
mWifiP2pServiceRequest = WifiP2pDnsSdServiceRequest.newInstance();
}
private void startServiceDiscovery() {
mWifiP2pManager.removeServiceRequest(mWifiP2pChannel, mWifiP2pServiceRequest,
new WifiP2pManager.ActionListener() {
#Override
public void onSuccess() {
mWifiP2pManager.addServiceRequest(mWifiP2pChannel, mWifiP2pServiceRequest,
new WifiP2pManager.ActionListener() {
#Override
public void onSuccess() {
mWifiP2pManager.discoverServices(mWifiP2pChannel,
new WifiP2pManager.ActionListener() {
#Override
public void onSuccess() {
//service discovery started
mServiceDiscoveringHandler.postDelayed(
mServiceDiscoveringRunnable,
SERVICE_DISCOVERING_INTERVAL);
}
#Override
public void onFailure(int error) {
// react to failure of starting service discovery
}
});
}
#Override
public void onFailure(int error) {
// react to failure of adding service request
}
});
}
#Override
public void onFailure(int reason) {
// react to failure of removing service request
}
});
}
the mServiceDiscoveringRunnable was just:
private Runnable mServiceDiscoveringRunnable = new Runnable() {
#Override
public void run() {
startServiceDiscovery();
}
};
All this made my system work quite well. It wasn't perfect yet, but with the lack of documentation on this subject I think I couldn't do much more to improve it.
If you test this approach, be sure to tell me how it works for you (or if it works for you ;) ).
if the problem is the detection of the service i believe that crearing group is the best way to make the device and service detectable but the if created group in the all devices then you cannot connect in direct.
but as wifi network.
i do it every day and it works.
I connect to the node server with socketio.SocketIO running as a service.And, When Service restarts,opens socket.io without socket.io closure.That's a problem.
A device making multiple socketIO connection on the server side.So the server is swelling..
! I am using gottox/socketio-java-client on android.
Check Socket is connected or not using socket.isConnected().
This will return true if socket is connected
Its just an idea so i don't know the limitations. pls let me know.
You can ping the server to check if the connection is alive.
In android
socket.emit("ping","ping");
socket.on("pong",pingHandler); //EmitterListener
private Emitter.Listener pingHandler=new Emitter.Listener(){
#Override
public void call(final Object... args) {
Log.d("SocketStatus","Connection is active");
});
}
and make the server return response for the ping
socket.on("ping",function(data){
socket.emit("pong","pong"); //from your server ex.Node.js
});
You can check the socket.connected property:
var socket = io.connect();
console.log('Connected status before onConnect', socket.socket.connected);
socket.on('connect', function() {
console.log('Connected status onConnect', socket.socket.connected);
});
It's updated dynamically, if the connection is lost it'll be set to false until the client picks up the connection again. So easy to check for with setInterval or something like that.
Another solution would be to catch disconnect events and track the status yourself.
The following is an expansion/modification of Rafique Mohammed answer above. The correct way is to try to reconnect on client side.
Internet drops (server cannot communicate disconnection to client). Server crashes (server may/may not be able to tell client. Server Restart (server can tell but that just extra work). After reconnection you will also like to rejoin the room for seamless communication
public void connectAfterDisconnectSocket(String senderActivity) {
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
boolean isConnected = false;
isConnected = mSocket != null && mSocket.connected();
if (!isConnected) {
SocketIOClient socketIOClient = new SocketIOClient();
socketIOClient.connectToSocketIO();
if (senderActivity.equals("A")) {
A.joinChatRoom(room);
}
if (senderActivity.equals("B")) {
B.joinChatRoom(room);
}
}
}
}, 0, 1000); //put here time 1000 milliseconds=1 second
}