Android 4.3 で Bluetooth デバイスに接続する必要があるアプリケーションを開発しています。
BluetoothGatt.readRemoteRssi() を使用して、BLE デバイスに接続し、デバイスから RSSI を読み取ることができます。
また、接続した複数のデバイスの RSSI を一度に読みたいのですが、前回接続したデバイスの BLE デバイスの RSSI しか読み込めません。
2 つの BLE デバイス A と B がある場合、デバイス Aに接続し、そこから RSSI を読み取りました。デバイス Bに接続した後、デバイス Bから RSSI を読み取ることができます。ただし、デバイス Aの RSSI は読み取らず、デバイス Bからの RSSI のみを読み取ります。
Main.javaには、接続したすべてのデバイスがリストされています。
リスト上のデバイスをクリックすると、デバイス名とアドレスが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);
}
});
そして、DeviceControl.javaはBluetoothLeService.javaを呼び出してデバイスに接続します。
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;
}
};
BluetoothLeService.javaがデバイスに接続します。
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;
}
デバイスに接続したら、readRemoteRssi を使用してデバイスから RSSI を読み取ることができます。
public void readRemoteRssi() {
mBluetoothGatt.readRemoteRssi();
}
しかし、最後に接続したデバイスの RSSI しか読み取れませんでした。
ログを見ると、常に、最後に接続したデバイスにonCharacteristicWriteと readRemoteRssi()が送信されます。
RSSI を読み取る前、または CharacteristicWrite 値を最初のデバイスに書き込む前に、GATT を再接続するか、デバイスを最初のアドレスに再接続する必要がありますか??
接続したすべてのデバイスの RSSI を読み取る他の方法はありますか??