[英]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.