Android BluetoothGattServer特性:can';t启用BluetoothGattCharacteristic通知
我正在尝试用我的Android设备创建一个外围蓝牙LE。我还想随信寄一封信 因此,我采取了以下步骤: 1-创建具有NOTIFY属性和读取权限的BluetoothGattCharacteristic 2-添加以字节为单位编码的字符串值 3-创建BluetoothGattService并将特性添加到此服务 4-创建BluetoothGattServer并向服务器添加服务 5-启动BluetoothLeAdvertiser 代码如下:Android BluetoothGattServer特性:can';t启用BluetoothGattCharacteristic通知,android,ios,bluetooth,Android,Ios,Bluetooth,我正在尝试用我的Android设备创建一个外围蓝牙LE。我还想随信寄一封信 因此,我采取了以下步骤: 1-创建具有NOTIFY属性和读取权限的BluetoothGattCharacteristic 2-添加以字节为单位编码的字符串值 3-创建BluetoothGattService并将特性添加到此服务 4-创建BluetoothGattServer并向服务器添加服务 5-启动BluetoothLeAdvertiser 代码如下: private void advertise() { S
private void advertise() {
String toBeSent = "I'm an Android" + "EOM";
BluetoothGattCharacteristic ccc = new BluetoothGattCharacteristic(
UUID.fromString(getString(R.string.ble_uuid)),
BluetoothGattCharacteristic.PROPERTY_NOTIFY,
BluetoothGattCharacteristic.PERMISSION_READ
);
BluetoothGattService service = new BluetoothGattService(UUID.fromString(getString(R.string.ble_uuid)), BluetoothGattService.SERVICE_TYPE_PRIMARY);
byte[] bytes = toBeSent.getBytes(StandardCharsets.UTF_8);
ccc.setValue(bytes);
BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(UUID.fromString(getString(R.string.ble_uuid)), BluetoothGattDescriptor.PERMISSION_READ);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
ccc.addDescriptor(descriptor);
service.addCharacteristic(ccc);
BluetoothManager manager = (BluetoothManager) getSystemService (Context.BLUETOOTH_SERVICE);
BluetoothGattServer server = manager.openGattServer(this, new BluetoothGattServerCallback() {
@Override
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
super.onConnectionStateChange(device, status, newState);
Log.i("onConnectionStateChange", "Status: " + status);
switch (newState) {
case BluetoothProfile.STATE_CONNECTED:
Log.i("gattCallback", "STATE_CONNECTED");
break;
case BluetoothProfile.STATE_DISCONNECTED:
Log.e("gattCallback", "STATE_DISCONNECTED");
found = false;
break;
default:
Log.e("gattCallback", "STATE_OTHER");
}
}
@Override
public void onServiceAdded(int status, BluetoothGattService service) {
super.onServiceAdded(status, service);
}
@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 onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
super.onCharacteristicWriteRequest(device, requestId, characteristic, preparedWrite, responseNeeded, offset, value);
}
});
server.addService(service);
BluetoothLeAdvertiser advertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
AdvertiseSettings settings = new AdvertiseSettings.Builder()
.setAdvertiseMode( AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY )
.setTxPowerLevel( AdvertiseSettings.ADVERTISE_TX_POWER_HIGH )
.setConnectable(true)
.build();
ParcelUuid pUuid = new ParcelUuid( UUID.fromString( getString( R.string.ble_uuid ) ) );
AdvertiseData data = new AdvertiseData.Builder().setIncludeDeviceName(false).addServiceUuid( pUuid )
.build();
AdvertiseCallback advertisingCallback = new AdvertiseCallback() {
@Override
public void onStartSuccess(AdvertiseSettings settingsInEffect) {
super.onStartSuccess(settingsInEffect);
}
@Override
public void onStartFailure(int errorCode) {
super.onStartFailure(errorCode);
}
};
advertiser.startAdvertising( settings, data, advertisingCallback );
}
我正在尝试在iOS设备上以可恢复的中心模式读取此服务器的特征。我可以发现并连接到Android外围设备,但我无法读取特征值,因为通知标志(或位)似乎没有被监听。
换句话说,iOS设备可以看到特征,但启用特征通知的方法似乎不起作用
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
iOS central会找到该特征,但不会调用通知委托,因为找不到NOTIFY属性
我曾尝试将这个iOS central连接到一个启用了characteristic的NOTIFY属性的iOS外围设备,并且一切正常,所以我的第一个想法是我在Android中缺少了一些东西。Android部分完全正确吗
为了更清楚,这里有从Android开始到iOS的特性:
您应该添加字符的描述符。请参阅@郭兴民如何添加描述符?我使用了
BluetoothGattDescriptor描述符=新的BluetoothGattDescriptor(UUID.fromString(getString(R.string.ble_uid)),BluetoothGattDescriptor.PERMISSION_WRITE | BluetoothGattDescriptor.PERMISSION_READ);描述符.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);添加描述符(描述符)代码>没有更改。在你的代码中我看不到任何描述符上瘾。你会发布btsnoop日志还是adb日志。我已经发布了我的两个特征:一个是从Android开始的,另一个是从iOS开始的。亚洲开发银行对此只字不提。只有连接状态。我现在不能窥探BT。我认为Android中缺少了一些我不知道的特性写入。你应该添加该特性的描述符。参考@郭兴民我应该如何添加描述符?我使用了BluetoothGattDescriptor描述符=新的BluetoothGattDescriptor(UUID.fromString(getString(R.string.ble_uid)),BluetoothGattDescriptor.PERMISSION_WRITE | BluetoothGattDescriptor.PERMISSION_READ);描述符.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);添加描述符(描述符)代码>没有更改。在你的代码中我看不到任何描述符上瘾。你会发布btsnoop日志还是adb日志。我已经发布了我的两个特征:一个是从Android开始的,另一个是从iOS开始的。亚洲开发银行对此只字不提。只有连接状态。我现在不能窥探BT。我认为Android中缺少了一些我不知道的东西。