在服务器端,我设置:
val bluetoothLeAdvertiser: BluetoothLeAdvertiser = mBleAdapter!!.bluetoothLeAdvertiser
bluetoothLeAdvertiser.startAdvertising(settings, advertiseData, scanResponseData, callback)
}
private var mGattServer:BluetoothGattServer? = null
private fun initServices() {
mGattServer = mBleManager?.openGattServer(this, gattServerCallback) as BluetoothGattServer
val gattService = BluetoothGattService(Constants.BLE_SERVICE_UUID, BluetoothGattService.SERVICE_TYPE_PRIMARY)
val characteristicRead = BluetoothGattCharacteristic(Constants.BLE_READ_UUID, BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_READ)
val descriptor = BluetoothGattDescriptor(Constants.BLE_DESC_UUID, BluetoothGattCharacteristic.PERMISSION_WRITE)
characteristicRead.addDescriptor(descriptor)
gattService.addCharacteristic(characteristicRead)
val characteristicWrite = BluetoothGattCharacteristic(Constants.BLE_WRITE_UUID, BluetoothGattCharacteristic.PROPERTY_WRITE or
BluetoothGattCharacteristic.PROPERTY_READ or BluetoothGattCharacteristic.PROPERTY_NOTIFY,
BluetoothGattCharacteristic.PERMISSION_WRITE)
gattService.addCharacteristic(characteristicWrite)
mGattServer?.addService(gattService)
}和UUIDS定义为:
val BLE_SERVICE_UUID = UUID.fromString("00001802-0000-1000-8000-00805f9b34fb")
val BLE_WRITE_UUID = UUID.fromString("00002a02-0000-1000-8000-00805f9b34fb")
val BLE_READ_UUID = UUID.fromString("00002a03-0000-1000-8000-00805f9b34fb")
val BLE_DESC_UUID = UUID.fromString("00002a04-0000-1000-8000-00805f9b34fb")当运行服务器应用程序时,它会打印onServiceAdded:status=0,这意味着添加服务是正确的。
但是当客户端应用程序连接到GattServer并打印所有服务中的所有特征时,我发现:
Connected to GATT server.
SUUID:00001801-0000-1000-8000-00805f9b34fb
CUUID:00002a05-0000-1000-8000-00805f9b34fb Properties:32
SUUID:00001800-0000-1000-8000-00805f9b34fb
CUUID:00002a00-0000-1000-8000-00805f9b34fb Properties:2
CUUID:00002a01-0000-1000-8000-00805f9b34fb Properties:2
CUUID:00002aa6-0000-1000-8000-00805f9b34fb Properties:2没有与我在服务器端设置的UUID相同的UUID,并且没有UUID具有BluetoothGattCharacteristic.PERMISSION_WRITE属性。
为什么?如何将数据从客户端发送到服务器?
发布于 2020-04-16 17:38:36
使用以下代码扫描,可以得到正确的结果:
List<ScanFilter> filters = new ArrayList<>();
ScanFilter scanFilter = new ScanFilter.Builder()
.setServiceUuid(new ParcelUuid(Constants.SERVICE_UUID))
.build();
filters.add(scanFilter);
ScanSettings settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)
.build();
/*mScanResults = new HashMap<>();
mScanCallback = new BtleScanCallback(mScanResults);*/
mScanCallback = new BtleScanCallback();
mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
mBluetoothLeScanner.startScan(filters, settings, mScanCallback);https://stackoverflow.com/questions/60859756
复制相似问题