2016-04-10 6 views
0

私は、BluetoothLeGattのサンプルコードを使用して、アプリケーションの起動時にボンディングされたBLE周辺機器に自動的に接続するアプリケーションを作成しました。今私は、周辺機器の特性の1つからのデータをtextViewに表示しようとしています。 BluetoothLeGattのサンプルコードでは、ExpandableListView.OnChildClickListenerを使用してこれを実証するだけです。私のアプリはユーザー入力を必要とせず、単に特性からデータを取得する必要があります。これは私が今までに持っているものです:BLEデータをtextViewに表示

private TextView mConnectionState; 
private TextView mDataField; 
private String mDeviceName; 
private String mDeviceAddress; 
private ExpandableListView mGattServicesList; 
private BluetoothLeService mBluetoothLeService; 
private boolean mConnected = false; 
private BluetoothGattCharacteristic mNotifyCharacteristic; 

private final String LIST_NAME = "NAME"; 
private final String LIST_UUID = "UUID"; 

// Code to manage Service lifecycle. 
private final ServiceConnection mServiceConnection = new ServiceConnection() { 

    @Override 
    public void onServiceConnected(ComponentName componentName, IBinder service) { 
     mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService(); 
     if (!mBluetoothLeService.initialize()) { 
      Log.e(TAG, "Unable to initialize Bluetooth"); 
      finish(); 
     } 
     // Automatically connects to the device upon successful start-up initialization. 
     mBluetoothLeService.connect(mDeviceAddress); 
    } 

    @Override 
    public void onServiceDisconnected(ComponentName componentName) { 
     mBluetoothLeService = null; 
    } 
}; 

// Handles various events fired by the Service. 
// ACTION_GATT_CONNECTED: connected to a GATT server. 
// ACTION_GATT_DISCONNECTED: disconnected from a GATT server. 
// ACTION_GATT_SERVICES_DISCOVERED: discovered GATT services. 
// ACTION_DATA_AVAILABLE: received data from the device. This can be a result of read 
//      or notification operations. 
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     final String action = intent.getAction(); 
     if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) { 
      mConnected = true; 
      updateConnectionState(R.string.connected); 
      mConnectionState.setTextColor(Color.parseColor("#FF17AA00")); 
      invalidateOptionsMenu(); 
     } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) { 
      mConnected = false; 
      updateConnectionState(R.string.disconnected); 
      invalidateOptionsMenu(); 
      clearUI(); 
     } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) { 
      // Show all the supported services and characteristics on the user interface. 
      //displayGattServices(mBluetoothLeService.getSupportedGattServices()); 
     } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) { 
      displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA)); 
     } 
    } 
}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_device_control); 

    final Intent intent = getIntent(); 
    mDeviceName = intent.getStringExtra(EXTRAS_DEVICE_NAME); 
    mDeviceAddress = intent.getStringExtra(EXTRAS_DEVICE_ADDRESS); 
    mConnectionState = (TextView) findViewById(R.id.connection_state); 
    mDataField = (TextView) findViewById(R.id.data); 

    Intent gattServiceIntent = new Intent(this, BluetoothLeService.class); 
    bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 






} 
@Override 
protected void onResume() { 
    super.onResume(); 
    registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter()); 
    if (mBluetoothLeService != null) { 
     final boolean result = mBluetoothLeService.connect(mDeviceAddress); 
     Log.d(TAG, "Connect request result=" + result); 
    } 

} 

@Override 
protected void onPause() { 
    super.onPause(); 
    unregisterReceiver(mGattUpdateReceiver); 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    unbindService(mServiceConnection); 
    mBluetoothLeService = null; 
} 

private void updateConnectionState(final int resourceId) { 
    runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      mConnectionState.setText(resourceId); 
     } 
    }); 
} 

private void displayData(String data) { 
    if (data != null) { 
     mDataField.setText(data); 
    } 
} 

private void clearUI() { 
    mGattServicesList.setAdapter((SimpleExpandableListAdapter) null); 
    mDataField.setText(R.string.no_data); 
} 




private static IntentFilter makeGattUpdateIntentFilter() { 
    final IntentFilter intentFilter = new IntentFilter(); 
    intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED); 
    intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED); 
    intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED); 
    intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE); 
    return intentFilter; 
} 
+0

あなたの質問はありますか? – morynicz

+0

私は既にボンディングされたデバイスに正常に接続しましたが、今ではuuidを使って特性からデータを取得してtextViewに表示しようとしています。 BluetoothLeGattの例は、サポートされている特性を表示しているクリックリスナ上の展開可能なリストビューを使用して、ユーザーが特性をどのように選択するかを示しています。私はすべてをバイパスし、既知のuuidを持つ特性からデータを取得したいだけです。 – dyay108

答えて

0

それを実証しました。 サービスを繰り返すと、サービス後の特性が検出されました。

UUID chara = UUID.fromString("c97433f0-be8f-4dc8-b6f0-5343e6100eb4"); 
List<BluetoothGattService> servs = mBluetoothLeService.getSupportedGattServices(); 
      for (int i = 0; servs.size() > i; i++) { 

       List<BluetoothGattCharacteristic> charac = servs.get(i).getCharacteristics(); 
       for (int j = 0; charac.size() > i; i++) { 
        BluetoothGattCharacteristic ch = charac.get(i); 
        if (ch.getUuid() == chara) { 
         mBluetoothLeService.readCharacteristic(ch); 
         mBluetoothLeService.setCharacteristicNotification(ch, true); 

        } 
       } 
      } 
関連する問題