如何通过 BluetoothGatt 在两部 android 手机中相互通信
How to communicate with each other in two android phones through BluetoothGatt
在服务器端,我设置:
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 具有 BluetoothGattCharacteristic.PERMISSION_WRITE.
的属性
为什么?如何从客户端向服务器发送数据?
使用下面的代码扫描,可以得到正确的结果:
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);
在服务器端,我设置:
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 具有 BluetoothGattCharacteristic.PERMISSION_WRITE.
的属性为什么?如何从客户端向服务器发送数据?
使用下面的代码扫描,可以得到正确的结果:
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);