我的gattServer用配对的蓝牙设备播发一些数据,并且运行带有服务的gattServer。蓝牙处于打开状态时一切正常,但是我关闭了蓝牙并再次抛出异常
sGattServer.notifyCharacteristicChanged(device, getCharacteristic(Constants.NOTIFICATION_SOURCE), false);
这是我的连接方法
BluetoothAdapter bleAdapter = ((BluetoothManager) context.getSystemService(BLUETOOTH_SERVICE)).getAdapter();
final Set<BluetoothDevice> pairedDevices = bleAdapter.getBondedDevices();
for (BluetoothDevice d : pairedDevices) {
d.connectGatt(context, true, new BluetoothGattCallback() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onConnectionStateChange(BluetoothGatt
gatt, int status, int newState) {
switch (newState) {
case BluetoothProfile.STATE_CONNECTED:
gatt.getServices();
break;
case BluetoothProfile.STATE_DISCONNECTED:
if (gatt !=null){
gatt.close();
gatt.disconnect();
gatt.connect();
}
break;
}
}
});
}
在此处留下痕迹
10-23 10:04:53.978 27768-27768/E/BluetoothGattServer: android.os.DeadObjectException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:496)
at android.bluetooth.IBluetoothGatt$Stub$Proxy.sendNotification(IBluetoothGatt.java:1482)
at android.bluetooth.BluetoothGattServer.notifyCharacteristicChanged(BluetoothGattServer.java:539)
答案 0 :(得分:0)
关闭蓝牙后,Android会重新启动蓝牙堆栈以清理其状态。有点像用40磅的八角锤打碎核桃。参见Logcat
2019-08-02 11:56:29.274 10736-10736/? D/BluetoothAdapterService: onDestroy()
2019-08-02 11:56:29.281 10736-10736/? I/BluetoothAdapterService: Force exit to cleanup internal state in Bluetooth stack
在GattServer服务中,当蓝牙重新打开时,您需要重新创建 BluetoothGattServer 对象。
您没有显示问题所在的服务中的代码,但是您将需要执行以下操作。创建方法 createServerGattService ,该方法定义服务UUID和GATT服务器服务的特征,然后将其注册到BLE堆栈。您已经有了这个,因为您说GATT服务器可以正常工作,直到您关闭蓝牙适配器并打开它。
向您的服务添加蓝牙适配器电源状态接收器:
private BluetoothGattServer gattServer;
private final BroadcastReceiver m_bluetoothAdapterPowerStateeceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
BluetoothAdapter.ERROR);
switch (state) {
case BluetoothAdapter.STATE_OFF:
gattServer = null;
break;
case BluetoothAdapter.STATE_TURNING_OFF:
gattServer.close();
break;
case BluetoothAdapter.STATE_ON:
gattServer = createServerGattService();
break;
case BluetoothAdapter.STATE_TURNING_ON:
break;
}
}
}
};
在服务的 onCreate()方法中,如果蓝牙适配器已打开电源,则注册接收器并实例化gatt服务器:
@Override
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(m_bluetoothAdapterPowerStateeceiver, filter);
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON) {
gattServer = createServerGattService();
}
}
在服务的 onDestroy()方法中,卸下接收器并关闭GATT服务器连接:
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(m_bluetoothAdapterPowerStateeceiver);
if(gattServer != null)
gattServer.close();
}
为完整起见, createServerGattService()应该看起来像这样:
private BluetoothGattServer createServerGattService() {
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
BluetoothGattServer server = null;
if(bluetoothManager != null) {
server = bluetoothManager.openGattServer(this, new BluetoothGattServerCallback() {
@Override
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
super.onConnectionStateChange(device, status, newState);
}
@Override
public void onServiceAdded(int status, BluetoothGattService service) {
super.onServiceAdded(status, service);
}
@Override
public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicReadRequest(device, requestId, offset, characteristic);
}
@Override
public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
super.onCharacteristicWriteRequest(device, requestId, characteristic, preparedWrite, responseNeeded, offset, value);
}
@Override
public void onDescriptorReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattDescriptor descriptor) {
super.onDescriptorReadRequest(device, requestId, offset, descriptor);
}
@Override
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
super.onDescriptorWriteRequest(device, requestId, descriptor, preparedWrite, responseNeeded, offset, value);
}
@Override
public void onExecuteWrite(BluetoothDevice device, int requestId, boolean execute) {
super.onExecuteWrite(device, requestId, execute);
}
@Override
public void onNotificationSent(BluetoothDevice device, int status) {
super.onNotificationSent(device, status);
}
@Override
public void onMtuChanged(BluetoothDevice device, int mtu) {
super.onMtuChanged(device, mtu);
}
@Override
public void onPhyUpdate(BluetoothDevice device, int txPhy, int rxPhy, int status) {
super.onPhyUpdate(device, txPhy, rxPhy, status);
}
@Override
public void onPhyRead(BluetoothDevice device, int txPhy, int rxPhy, int status) {
super.onPhyRead(device, txPhy, rxPhy, status);
}
});
BluetoothGattService service = new BluetoothGattService(serviceUuid, BluetoothGattService.SERVICE_TYPE_PRIMARY);
BluetoothGattCharacteristic characteristic1 = new BluetoothGattCharacteristic(
characteristic1Uuid,
BluetoothGattCharacteristic.PROPERTY_READ,
BluetoothGattCharacteristic.PERMISSION_READ);
service.addCharacteristic(characteristic1);
server.addService(service);
}
return server;
}