2016-06-21 25 views
0

6.0.1アンドロイドデバイスからRN4020ブルートゥースモジュールにバイト配列を送信しようとしています。これまでのところ、私はモジュールを発見し、結合し、モジュールに接続することができました。私は応答なしで書き込みのための16ビットのUUIDを組み込んだバイト配列を送信しようとしています。コードを使用すると、エラーは発生しません。バイト配列は送信されません。私は間違って何をしていますか? BluetoothGattCharacteristicのコンストラクタが公開されている理由を私は知らないカスタムUUIDなしでBLEデバイスにbyte []を送信する方法

namespace Remote 
{ 
    [Activity(Label = "Remote", MainLauncher = true, Icon =  "@drawable/icon")] 
    public class MainActivity : Activity 
    { 
     int count = 1; 
     const string UniversalUUID = "00001101-0000-1000-8000-00805F9B34FB"; 
     const string MAC = "00:1E:C0:22:3D:0E"; 
     ICollection<BluetoothDevice> pairedDevices; 
     BluetoothAdapter adapter; 
     BluetoothDevice device; 
     BluetoothGatt gatt; 
     BluetoothGattCallback callBack; 
     BluetoothManager manager; 

     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 

      SetContentView(Resource.Layout.Main); 
      Button test = FindViewById<Button>(Resource.Id.Test); 
      EditText MACs = FindViewById<EditText>(Resource.Id.MACs); 

      pairedDevices = getAllPairedDevices(); 

      test.Click += delegate 
      { 
        connected(); 
        byte[] buffer = new byte [] { 124, 079, 044, 048, 052, 044, 048, 052, 013, 010};//stringToByteArray("|O,04,04\r"); 
        BluetoothGattCharacteristic characteristic = new  BluetoothGattCharacteristic(UUID.NameUUIDFromBytes(new byte[] { 0, 4}),  GattProperty.Write, GattPermission.Write); 
        characteristic.SetValue(buffer); 
        gatt.WriteCharacteristic(characteristic); 
      }; 


     private ICollection<BluetoothDevice> getAllPairedDevices() 
     { 
      BluetoothAdapter btAdapter = BluetoothAdapter.DefaultAdapter; 
      return btAdapter.BondedDevices; 
     } 

     private async Task<bool> connected() 
     { 
      manager =  (BluetoothManager)GetSystemService(Context.BluetoothService); 
      adapter = manager.Adapter; 
      device = adapter.GetRemoteDevice(MAC); 
      gatt = device.ConnectGatt(this, false, callBack);  

      return true; 
     } 
    } 
} 

答えて

0

は、ここに私のコードです。 BluetoothGattCharacteristicを取得する正しい方法は、gatt.DiscoverServices()を呼び出してサービスの検出を行うことです。サービスディスカバリがコールバックされると、gatt.GetServices()によってすべてのサービスと特性を取得できます。

コードでは、接続するまで待つこともありません。 device.ConnectGattメソッドは非同期で、接続試行を開始します。コールバックオブジェクトのOnConnectionStateChangeは、デバイスの接続/切断時に呼び出されます。

+0

1コールバックを取得して成功イベントでメソッドの実行を開始する方法がわかりません。 2 gatt.GetServices()メソッドはすべてgatt.getService(UUID)です。 –

+0

私は一般的にブルートゥースとxamarinには新しく、1週間インターネットを精査しています。これを行う方法のxamarinの例が見つかりませんでした。この時点で私はかなり切望しています。だから誰かがバイト配列をUUIDに接続手順を含めて送信する方法のサンプルコードを与えてください。 –

+0

すべてのJavaの例が動作します。これはXamarinの方言と同じAPIです。たとえば、https://android.googlesource.com/platform/development/+/cefd49aae65dc85161d08419494071d74ffb982f/samples/BluetoothLeGatt/src/com/example/bluetooth/le/BluetoothLeService.javaを確認してください。 – Emil

関連する問題