我正在尝试使用我的Android设备创建外设蓝牙LE。我也想发送它的特色。
所以我做了这些步骤:
1-使用NOTIFY属性和READ权限创建BluetoothGattCharacteristic。
2-添加以字节编码的字符串值
3-创建BluetoothGattService并将特征添加到此服务
4-创建BluetoothGattServer并向服务器添加服务
5-启动BluetoothLeAdvertiser
以下是代码:
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 );
}
我正试图在BLE Central模式下在iOS设备上读取此服务器的特征。我可以发现并连接到Android外设,但我无法读取特征值,因为通知标志(或位)似乎没有被监听。 换句话说,iOS设备可以看到特征,但启用特征通知的方法似乎无法正常工作。
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
iOS中心找到特征,但不会调用通知委托,因为找不到NOTIFY属性。
我尝试将这个iOS中心与iOS外设连接起来,启用了特性的NOTIFY属性,一切运行良好,所以我首先想到的是我在Android中遗漏了一些东西。 Android部分完全正确吗?
为了更加清晰,这里的特征是从Android开始并到达iOS: