2017-12-19 18 views
0

。 Bluetooth Low Energyプロトコルに関する問題でかなり苦労しています。たとえば、デバイスにはサービスがあり、このサービスにはDescriptorを含むCharacteristicが含まれています。サービス、特性、記述子のUUIDはあらかじめわかっていません。私の質問は、この特定のUUIDがService/Charactersitic/Descriptorの一種であることを知っている方法で、それらのUUIDを取得する方法です。それは一緒にすべてのUUIDを返し、私たちはサービスに属する1わからないのでのBluetooth低エネルギー(BLE) - 私は誰かが役立つことを願ってどのようにサービスのUUIDを取得するには、特性と記述別々

BluetoothGatt.getServices()は、役に立ちません。私は、UUIDを分割する方法があると確信しています。少なくともnRF Connectアプリ(Playストアで見つけることができます)がこれを行うことができます。あなたの時間と支援するための努力のための

感謝。ここで

+0

BluetoothGatt.getServices()リモートデバイスが提供するGATTサービスのリストを返します。その後、各サービスの特性を得るためにサービスを通過することができます。 idkあなたが間違っていること。可能であれば、いくつかのコードを共有してください。 –

答えて

0

あなたは利用可能なすべての特性のリストを持っている:https://www.bluetooth.com/specifications/gatt/characteristics

今、あなたはのUUIDのリストを実行し、それらを比較することができます。あなたはガットコールバックから特性を受け取るとき

その後
// All BLE characteristic UUIDs are of the form: 
// 0000XXXX-0000-1000-8000-00805f9b34fb 
// The assigned number for the Heart Rate Measurement characteristic UUID is 
// listed as 0x2A37, which is how the developer of the sample code could arrive at: 
// 00002a37-0000-1000-8000-00805f9b34fb 
public static class Characteristic { 
    final static public UUID HEART_RATE_MEASUREMENT = UUID.fromString("00002a37-0000-1000-8000-00805f9b34fb"); 
    final static public UUID CSC_MEASUREMENT   = UUID.fromString("00002a5b-0000-1000-8000-00805f9b34fb"); 
    final static public UUID MANUFACTURER_STRING  = UUID.fromString("00002a29-0000-1000-8000-00805f9b34fb"); 
    final static public UUID MODEL_NUMBER_STRING  = UUID.fromString("00002a24-0000-1000-8000-00805f9b34fb"); 
    final static public UUID FIRMWARE_REVISION_STRING = UUID.fromString("00002a26-0000-1000-8000-00805f9b34fb"); 
    final static public UUID APPEARANCE    = UUID.fromString("00002a01-0000-1000-8000-00805f9b34fb"); 
    final static public UUID BODY_SENSOR_LOCATION  = UUID.fromString("00002a38-0000-1000-8000-00805f9b34fb"); 
    final static public UUID BATTERY_LEVEL   = UUID.fromString("00002a19-0000-1000-8000-00805f9b34fb"); 
    final static public UUID CLIENT_CHARACTERISTIC_CONFIG = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"); 
} 

、(リストと照合)をチェックしてみてくださいそれは特徴:

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { 
    ... 
    @Override 
    public void onCharacteristicRead(BluetoothGatt gatt, 
            BluetoothGattCharacteristic characteristic, 
            int status) { 
     if (status == BluetoothGatt.GATT_SUCCESS) { 
      getCharacteristicValue(characteristic); 
     } 
    } 

    @Override 
    public void onCharacteristicChanged(BluetoothGatt gatt, 
             BluetoothGattCharacteristic characteristic) { 
     getCharacteristicValue(characteristic); 
    } 
} 

private void getCharacteristicValue(BluetoothGattCharacteristic characteristic) { 
    if(characteristic.getUuid().equals(Characteristic.HEART_RATE_MEASUREMENT)) { 
     if (mType == Accessory.Type.HRM && mBtLeGattServiceHeartrate != null) { 
      mBtLeGattServiceHeartrate.onCharacteristicChanged(mContext, BtLeDevice.this, characteristic); 
     } 
    } 
} 

希望、これはここではそのうちのいくつかを含むクラスがあります助けてください。

+0

あなたの努力に感謝します。しかし、それは助けにはならない。私は事前にUUIDを知らない。目的はサービス、特性、記述子のためにそれらを個別に見つけることです。 –

0

問題を解決するために私のための唯一の方法は、ScanResultから取得されScanRecordを使用することでした。 ScanRecordには、サービスのUUIDを含む各スキャンされたデバイスに関する情報が保存されます。したがって

List<UUID> serviceUUIDsList  = new ArrayList<>(); 
List<UUID> characteristicUUIDsList = new ArrayList<>(); 
List<UUID> descriptorUUIDsList  = new ArrayList<>(); 

private void initScanning(BluetoothLeScannerCompat bleScanner) 
{ 
    bleScanner.startScan(getScanCallback()); 
} 

private ScanCallback getScanCallback() 
{ 
    return new ScanCallback() 
    { 
     @Override 
     public void onScanResult(int callbackType, ScanResult scanResult) 
     { 
      super.onScanResult(callbackType, scanResult); 
      serviceUUIDsList = getServiceUUIDsList(scanResult); 
     } 
    }; 
} 

private List<UUID> getServiceUUIDsList(ScanResult scanResult) 
{ 
    List<ParcelUuid> parcelUuids = scanResult.getScanRecord().getServiceUuids(); 

    List<UUID> serviceList = new ArrayList<>(); 

    for (int i = 0; i < parcelUuids.size(); i++) 
    { 
     UUID serviceUUID = parcelUuids.get(i).getUuid(); 

     if (!serviceList.contains(serviceUUID)) 
      serviceList.add(serviceUUID); 
    } 

    return serviceList; 
} 

、我々はサービスのUUIDを知っているとき、私たちは、特性と記述のUUIDを取得することができます:

private void defineCharAndDescrUUIDs(BluetoothGatt bluetoothGatt) 
{ 
    List<BluetoothGattService> servicesList = bluetoothGatt.getServices(); 

    for (int i = 0; i < servicesList.size(); i++) 
    { 
     BluetoothGattService bluetoothGattService = servicesList.get(i); 

     if (serviceUUIDsList.contains(bluetoothGattService.getUuid())) 
     { 
      List<BluetoothGattCharacteristic> bluetoothGattCharacteristicList = bluetoothGattService.getCharacteristics(); 

      for (BluetoothGattCharacteristic bluetoothGattCharacteristic : bluetoothGattCharacteristicList) 
      { 
       characteristicUUIDsList.add(bluetoothGattCharacteristic.getUuid()); 
       List<BluetoothGattDescriptor> bluetoothGattDescriptorsList = bluetoothGattCharacteristic.getDescriptors(); 

       for (BluetoothGattDescriptor bluetoothGattDescriptor : bluetoothGattDescriptorsList) 
       { 
        descriptorUUIDsList.add(bluetoothGattDescriptor.getUuid()); 
       } 
      } 
     } 
    } 
} 
をスキャンが initScanning()方法で開始し、 onScanResult()のいずれかの結果が返されたら、私たちは ScanRecordオブジェクトにアクセスすることができます

私はまた、同様の問題で苦労しそうだ他の人を助けることができる、願っています。

関連する問題