2016-04-15 11 views
6

BLEデバイスから特性を連続的に読み取ろうとしています。Android Bluetooth LE - 読み取り特性がSamsungで動作しない

私は私のサービス・クラスでのRunnableを作成しました:

private class BackgroundRunnableForRead implements Runnable 
    { 


     private volatile boolean isRunning = true ; 
     @Override 
     public void run() { 
      try { 
      BluetoothLeService.this.backgroundRunID = Thread.currentThread().getId(); 
      while(isRunning) { 

        List<BluetoothGattService> gattServices = BluetoothLeService.this.getSupportedGattServices(); 

        if (gattServices != null && gattServices.size() > 0) { 
         BluetoothGattCharacteristic characteristic = getCharacteristic(gattServices); 

         if (characteristic != null && (characteristic.getProperties() & 2) > 0) { 
          BluetoothLeService.this.readCharacteristic(characteristic); 
         } 
        } 
       } 
      } 
      catch(Exception e) 
      { 
       isRunning= false; 
       e.printStackTrace(); 
      } 

     } 

     public void kill() 
     { 
      this.isRunning = false; 
     } 
    } 

と私は呼び出していたサービスの成功の発見について:

public void startReadingCharacteristics() 
    { 
     System.out.println("BluetoothLeService.startReadingCharacteristics"); 
     this.mBackgroundRunnable = new BackgroundRunnableForRead(); 
     mReadThread = new Thread(mBackgroundRunnable); 
     mReadThread.start(); 

    } 

そして、これは、コールバック読んcharactericsに私である -

public void onCharacteristicRead(BluetoothGatt gatt, 
             BluetoothGattCharacteristic characteristic, 
             int status) { 
      System.out.println("BluetoothLeService.onCharacteristicRead" + status); 
      if (status == BluetoothGatt.GATT_SUCCESS) { 
       broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); 
      } 

     } 

アプリケーションは、Nexus 5、Nexus 4、Motorola Gで正常に動作します。

私はこのコードをSamsung S6で実行しても機能しません。onCharacteristicRead()は呼び出されません。

readCharacteristics()を呼び出すと、onCharacteristicReadが実行されるのを待つときに問題が発生することがあります。

+0

OSのバージョンとは何ですか? –

+0

Osバージョン - Android 5.1.1。 – Anukool

+0

コードを書式化してください –

答えて

0

commands are not stackedのため、一度に1つのgattコマンドを実行することをお勧めします。したがって、現在のコールバックを読み込んだ後に次の読み込みを呼び出すような仕組みを実装する必要があります。

gattコールバックは別のスレッドから来る可能性がありますが、コールバックに読み込み値を保存してそこから次の読み込みをトリガーすると問題はありません。

+0

onCharacteristicReadコールバックがトリガされたときにのみ特性の読み込みを試みました。しかし、私はどこかに何か間違っていると思いますか? – Anukool

+0

さて、まず、特性の読み込みコマンドを実行しないと、 'onCharacteristicRead'コールバックがトリガされるべきですか? – JPS

+0

最初に、サービスが検出されるとすぐに読み取り特性を実行しています。その後、最初にcharacteracterReadが呼び出されます。その後、私はonCharacteristicsReadで特性を読み込みますが、動作しません。 – Anukool

関連する問題