I am developing an application where I have to connect to Bluetooth device on Android 4.3.
I can connect to the BLE device and read the RSSI from the device by using BluetoothGatt.readRemoteRssi().
And I want to read the RSSI of multiple device at one time where I have connected
But I only can read the RSSI of BLE device which the device I connected last time.
If there has two BLE device A and B.
I connected to device A, and read RSSI from it.
After I connected to device B, and I can read the RSSI from device B.
But it doesn't read the RSSI of device A, it only read the RSSI from device B.
In Main.java , it list all the device where I have connected.
When I click the device which on the list, it transmit the device name and address to DeviceControl.java.
final Intent qintent = new Intent(this, DeviceControl.class);
devicelist.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
HashMap<String, Object> select = (HashMap<String, Object>) devicelist.getItemAtPosition(arg2);
String name = (String) select.get("name");
String address = (String) select.get("address");
qintent.putExtra(DeviceControl.EXTRAS_DEVICE_NAME, name);
qintent.putExtra(DeviceControl.EXTRAS_DEVICE_ADDRESS, address);
startActivity(qintent);
}
});
And the DeviceControl.java will call the BluetoothLeService.java and connect to device.
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
// TODO Auto-generated method stub
mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
if(!mBluetoothLeService.initialize()) {
Log.e(TAG, "Unable to initialize Bluetooth");
finish();
}
registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
mBluetoothLeService.connect(mDeviceAddress);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
// TODO Auto-generated method stub
mBluetoothLeService = null;
}
};
And the BluetoothLeService.java will connect to the device.
public boolean connect(final String address) {
if (mBluetoothAdapter == null || address == null) {
Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
return false;
}
if(mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress)
&& mBluetoothGatt != null) {
Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
if(mBluetoothGatt.connect()) {
mConnectionState = STATE_CONNECTING;
return true;
}else {
return false;
}
}
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
if(device == null) {
Log.w(TAG, "Device not found. Unable to connect");
return false;
}
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
Log.d(TAG, "Try to create a new connection");
mBluetoothDeviceAddress = address;
mConnectionState =STATE_CONNECTING;
return true;
}
After it connect to the Device, I can use readRemoteRssi to read the RSSI from device.
public void readRemoteRssi() {
mBluetoothGatt.readRemoteRssi();
}
But it only read the RSSI of the last one device that I have connected.
When I see the Log, it always send onCharacteristicWrite and readRemoteRssi() to the last one device that I have connected.
Should I reconnect GATT or reconnect device to the first address before I want to read the RSSI or write the CharacteristicWrite value to first device ??
Does it has other method to read the RSSI of all the device that I have connected ??