当蓝牙关闭时,Android 会重新启动蓝牙堆栈以清理其状态。有点像用 40 磅的大锤敲碎核桃。查看日志猫
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;
}