0

私はBLEを使ってデータを送受信しています。Xamarin OnCharacteristicChanged

私はBLEにデータを書き込むことができますが、応答は来ません。"OnCharacteristicChanged"OnCharacteristicWriteに応答がありますが、bluetoothgatt WriteCharacteristicに書き込んでいます。

私を助けてください。私にベストプラクティスまたはサンプルコードを提案してください。 ありがとうございます。

ここに私のコードです。

onDiscovered service 
{ 
    string ser = "0003CDD0-0000-1000-8000-00805f9b0131"; 
        string charId = "0003CDD2-0000-1000-8000-00805f9b0131"; 
        bluetoothGatt = gatt; 
        //bleMTU = bluetoothGatt.RequestMtu(400); 
        mService = bluetoothGatt.GetService(UUID.FromString(ser)); 
        charbt = mService.GetCharacteristic(UUID.FromString(charId)); 
        bluetoothGatt.SetCharacteristicNotification(charbt, true); 
} 

Void writetosocket 
{ 
byte[] smallPacket = new byte[numberOfBytes]; 
charbt.SetValue(smallPacket); 
charbt.WriteType = BluetoothGattCharacteristic.WriteTypeNoResponse; 
bool check = bluetoothGatt.WriteCharacteristic(charbt); 
} 

答えて

0

ReadCharacteristicを購読していないようです。私のカスタムGattCallbackでこの部分をどのように実装したかは次のとおりです。

class GattCallback : BluetoothGattCallback 
{ 
    public BluetoothGattCharacteristic ReadCharacteristic { get; set; } 
    public BluetoothGattCharacteristic WriteCharacteristic { get; set; } 

    public override void OnServicesDiscovered(BluetoothGatt gatt, GattStatus status) 
    { 
     var service = gatt.Services.FirstOrDefault(s => s.Uuid.ToString().Equals("0003CDD0-0000-1000-8000-00805f9b0131")); 

     WriteCharacteristic = service.Characteristics[0]; //Find your WriteCharacteristic 
     ReadCharacteristic = service.Characteristics[1]; //Find your ReadCharacteristic 

     gatt.SetCharacteristicNotification(ReadCharacteristic, true); 

     //Subscribe on ReadCharacteristic for communication 
     var readCharacteristicDiscriptor = ReadCharacteristic.Descriptors[0]; 
     readCharacteristicDiscriptor.SetValue(BluetoothGattDescriptor.EnableIndicationValue.ToArray()); 

     gatt.WriteDescriptor(readCharacteristicDiscriptor); 
    } 
} 
関連する問題