[英]Android: How to tell if two phones are within approximately a foot of each other
[英]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,并且没有 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.