我编写了一个可以宣传和设置 gatt 服务器的 android 应用程序(外围设备)。当我使用其他 android 设备(Central)连接到它时,例如使用nRFConnect
application 或LightBlue
,Peripheral 端在收到连接回调后立即断开连接。
这是我设置 gatt 服务器的代码:
// Create our primary service
BluetoothGattService primaryService = new BluetoothGattService(
UUIDs.UUID_SERVICE,
BluetoothGattService.SERVICE_TYPE_PRIMARY
);
// Create our write characteristic
BluetoothGattCharacteristic writeCharacteristic = new BluetoothGattCharacteristic(
UUIDs.UUID_CHARACTERISTIC,
BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED_MITM | BluetoothGattCharacteristic.PERMISSION_WRITE_ENCRYPTED_MITM
);
// [START Add Client Characteristic Configuration]
BluetoothGattDescriptor descriptor1 = new BluetoothGattDescriptor(
UUIDs.UUID_CLIENT_CHARACTERISTIC_CONFIGURATION,
(BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED_MITM | BluetoothGattDescriptor.PERMISSION_WRITE_ENCRYPTED_MITM)
);
descriptor1.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
writeCharacteristic.addDescriptor(descriptor1);
// [END Add Client Characteristic Configuration]
// [START Add Characteristic User Description]
BluetoothGattDescriptor descriptor2 = new BluetoothGattDescriptor(
UUIDs.UUID_CHARACTERISTIC_USER_DESCRIPTION,
(BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED_MITM | BluetoothGattDescriptor.PERMISSION_WRITE_ENCRYPTED_MITM)
);
try
{
descriptor2.setValue((new String("Sample Description")).getBytes("UTF-8"));
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
writeCharacteristic.addDescriptor(descriptor2);
// [END Add Characteristic User Description]
// Add the write characteristic to service
if (primaryService != null)
{
// Add the characteristic to service
primaryService.addCharacteristic(writeCharacteristic);
}
else
{
// TODO: handle this state...
}
// Add the primary service to gatt server
if (mGattServer != null)
{
// Add the service to gatt server
mGattServer.addService(primaryService);
}
else
{
// TODO: handle this state...
}
我的BluetoothGattServerCallback
:
public void onConnectionStateChange(BluetoothDevice device, int status, int newState)
{
Log.d(TAG, "Our gatt server connection state changed, new state: " + Integer.toString(newState));
super.onConnectionStateChange(device, status, newState);
switch (newState)
{
case BluetoothProfile.STATE_CONNECTED:
Log.d(TAG, "Connection State: STATE_CONNECTED");
// Stop the advertisement
//BLEManager.getInstance().stopAdvertising();
// todo later...
break;
case BluetoothProfile.STATE_DISCONNECTED:
Log.d(TAG, "Connection State: STATE_DISCONNECTED");
// Start the advertisement again
//BLEManager.getInstance().startAdvertising();
// todo later...
break;
case BluetoothProfile.STATE_CONNECTING:
Log.d(TAG, "Connection State: STATE_CONNECTING");
// Nothing
break;
case BluetoothProfile.STATE_DISCONNECTING:
Log.d(TAG, "Connection State: STATE_DISCONNECTING");
// Nothing
break;
default:
// Nothing
}
if (null != mConnectionCallback && BluetoothGatt.GATT_SUCCESS == status)
{
mConnectionCallback.onConnectionStateChange(device, newState);
}
}
这是我的外围设备日志:
03-04 11:37:05.115 BluetoothGattServer: onServerConnectionState() - status=0 serverIf=5 device=5B:95:51:3C:AC:96
03-04 11:37:05.118 GattServerCallback: Our gatt server connection state changed, new state: 2
03-04 11:37:05.118 GattServerCallback: Connection State: STATE_CONNECTED
03-04 11:37:06.975 BluetoothGattServer: onServerConnectionState() - status=0 serverIf=5 device=5B:95:51:3C:AC:96
03-04 11:37:06.975 GattServerCallback: Our gatt server connection state changed, new state: 0
03-04 11:37:06.975 GattServerCallback: Connection State: STATE_DISCONNECTED
03-04 11:37:07.476 BluetoothGattServer: close()
03-04 11:37:07.476 BluetoothGattServer: unregisterCallback() - mServerIf=5
关于如何宣传和设置 gatt 服务器并没有什么晦涩难懂的,但我很困惑!我无法BLE Connection Parameters
通过 Android Peripheral API 或任何其他低级配置设置,所以一切都在这里!
有任何想法可以帮助我吗?