今現在Androidアプリでbluetoothを扱おうと考え触っていたのですが、詳しい仕組みがわからず困っています。現在のサンプルコードでは接続が確認されると乱数が表示されるようになっています。
やりたいこと
Androidアプリでセンサデータを読み込み表示する。
センサ SONY MESH LEDタグ
Androidのバージョンは5.0.2です。
MESHのハック方法に下記のやり方が書いてあったのですが
72C90005-57A9-4D40-B746-534E22EC9F9E (Indicate) の通知を有効にする。
72C90004-57A9-4D40-B746-534E22EC9F9E (Write) に0x00020103を書き込む。
72C90003-57A9-4D40-B746-534E22EC9F9E (Notify) の通知を有効にする。
Writeに書き込む方法がよくわかりません。
下のように入れてみましたが反応しませんでした。
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { // キャラクタリスティックのUUIDをチェック(getUuidの結果が全て小文字で帰ってくるのでUpperCaseに変換) if (CHARACTERISTIC_UUID.equals(characteristic.getUuid().toString().toUpperCase())) { // Peripheralで値が更新されたらNotificationを受ける. mStrReceivedNum = characteristic.getStringValue(0); // メインスレッドでTextViewに値をセットする. mBleHandler.sendEmptyMessage(MESSAGE_NEW_RECEIVEDNUM); byte level = 2; BluetoothGattCharacteristic c = characteristic(SERVICE_UUID, CHARACTERISTIC_UUIDW); c.setValue(new byte[] { level }); gatt.writeCharacteristic(c); } }
上のやり方で書き込みは可能でしょうか?
可能ならば場所はどこに入れれば良いでしょうか?
http://mslgt.hatenablog.com/entry/2015/05/17/212257
こちらのサイトのソースコードを使用しています。
java
1// 対象のサービスUUID. 2 private static final String SERVICE_UUID = "72C90001-57A9-4D40-B746-534E22EC9F9E"; 3 // キャラクタリスティックUUID. 4 private static final String CHARACTERISTIC_UUID = "72C90003-57A9-4D40-B746-534E22EC9F9E"; 5 private static final String CHARACTERISTIC_UUIDW ="72C90004-57A9-4D40-B746-534E22EC9F9E"; 6 private static final String CHARACTERISTIC_UUIDN ="72C90005-57A9-4D40-B746-534E22EC9F9E"; 7 // キャラクタリスティック設定UUID(固定値). 8 private static final String CHARACTERISTIC_CONFIG_UUID = "00002902-0000-1000-8000-00805f9b34fb"; 9 10 // 乱数送信用. 11 private Random mRandom = new Random(); 12 private Timer mTimer; 13 private SendDataTimer mSendDataTimer; 14 15 private final LeScanCallback mScanCallback = new BluetoothAdapter.LeScanCallback() { 16 @Override 17 public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { 18 runOnUiThread(new Runnable() { 19 @Override 20 public void run() { 21 // スキャン中に見つかったデバイスに接続を試みる.第三引数には接続後に呼ばれるBluetoothGattCallbackを指定する. 22 mBleGatt = device.connectGatt(getApplicationContext(), false, mGattCallback); 23 } 24 }); 25 } 26 }; 27 private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { 28 @Override 29 public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) 30 { 31 // 接続状況が変化したら実行. 32 if (newState == BluetoothProfile.STATE_CONNECTED) { 33 // 接続に成功したらサービスを検索する. 34 gatt.discoverServices(); 35 } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { 36 // 接続が切れたらGATTを空にする. 37 if (mBleGatt != null) 38 { 39 mBleGatt.close(); 40 mBleGatt = null; 41 } 42 mIsBluetoothEnable = false; 43 } 44 } 45 @Override 46 public void onServicesDiscovered(BluetoothGatt gatt, int status) 47 { 48 // Serviceが見つかったら実行. 49 if (status == BluetoothGatt.GATT_SUCCESS) { 50 // UUIDが同じかどうかを確認する. 51 BluetoothGattService service = gatt.getService(UUID.fromString(SERVICE_UUID)); 52 if (service != null) 53 { 54 // 指定したUUIDを持つCharacteristicを確認する. 55 mBleCharacteristic = service.getCharacteristic(UUID.fromString(CHARACTERISTIC_UUID)); 56 57 if (mBleCharacteristic != null) { 58 // Service, CharacteristicのUUIDが同じならBluetoothGattを更新する. 59 mBleGatt = gatt; 60 61 // キャラクタリスティックが見つかったら、Notificationをリクエスト. 62 boolean registered = mBleGatt.setCharacteristicNotification(mBleCharacteristic, true); 63 64 // Characteristic の Notificationを有効化する. 65 BluetoothGattDescriptor descriptor = mBleCharacteristic.getDescriptor( 66 UUID.fromString(CHARACTERISTIC_CONFIG_UUID)); 67 68 descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 69 mBleGatt.writeDescriptor(descriptor); 70 // 接続が完了したらデータ送信を開始する. 71 mIsBluetoothEnable = true; 72 } 73 } 74 } 75 } 76 @Override 77 public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) 78 { 79 // キャラクタリスティックのUUIDをチェック(getUuidの結果が全て小文字で帰ってくるのでUpperCaseに変換) 80 if (CHARACTERISTIC_UUID.equals(characteristic.getUuid().toString().toUpperCase())) 81 { 82 // Peripheralで値が更新されたらNotificationを受ける. 83 mStrReceivedNum = characteristic.getStringValue(0); 84 // メインスレッドでTextViewに値をセットする. 85 mBleHandler.sendEmptyMessage(MESSAGE_NEW_RECEIVEDNUM); 86 } 87 } 88 }; 89 private Handler mBleHandler = new Handler() 90 { 91 public void handleMessage(Message msg) 92 { 93 // UIスレッドで実行する処理. 94 switch (msg.what) 95 { 96 case MESSAGE_NEW_RECEIVEDNUM: 97 mTxtReceivedNum.setText(mStrReceivedNum); 98 break; 99 case MESSAGE_NEW_SENDNUM: 100 mTxtSendNum.setText(mStrSendNum); 101 break; 102 } 103 } 104 }; 105 106 @Override 107 protected void onCreate(Bundle savedInstanceState) { 108 super.onCreate(savedInstanceState); 109 setContentView(R.layout.activity_central); 110 111 mIsBluetoothEnable = false; 112 113 // Writeリクエストで送信する値、Notificationで受け取った値をセットするTextView. 114 mTxtReceivedNum = (TextView) findViewById(R.id.received_num); 115 mTxtSendNum = (TextView) findViewById(R.id.send_num); 116 117 // Bluetoothの使用準備. 118 mBleManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); 119 mBleAdapter = mBleManager.getAdapter(); 120 121 // Writeリクエスト用のタイマーをセット. 122 mTimer = new Timer(); 123 mSendDataTimer = new SendDataTimer(); 124 // 第二引数:最初の処理までのミリ秒 第三引数:以降の処理実行の間隔(ミリ秒). 125 mTimer.schedule(mSendDataTimer, 500, 1000); 126 127 // BluetoothがOffならインテントを表示する. 128 if ((mBleAdapter == null) 129 || (! mBleAdapter.isEnabled())) { 130 Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 131 // Intentでボタンを押すとonActivityResultが実行されるので、第二引数の番号を元に処理を行う. 132 startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 133 } 134 else 135 { 136 // BLEが使用可能ならスキャン開始. 137 this.scanNewDevice(); 138 } 139 } 140 @Override 141 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 142 // Intentでユーザーがボタンを押したら実行. 143 super.onActivityResult(requestCode, resultCode, data); 144 switch (requestCode) { 145 case REQUEST_ENABLE_BT: 146 if ((mBleAdapter != null) 147 || (mBleAdapter.isEnabled())) { 148 // BLEが使用可能ならスキャン開始. 149 this.scanNewDevice(); 150 } 151 break; 152 } 153 } 154 private void scanNewDevice() 155 { 156 // OS ver.5.0以上ならBluetoothLeScannerを使用する. 157 if (Build.VERSION.SDK_INT >= SDKVER_LOLLIPOP) 158 { 159 this.startScanByBleScanner(); 160 } 161 else 162 { 163 // デバイスの検出. 164 mBleAdapter.startLeScan(mScanCallback); 165 } 166 } 167 @TargetApi(SDKVER_LOLLIPOP) 168 private void startScanByBleScanner() 169 { 170 mBleScanner = mBleAdapter.getBluetoothLeScanner(); 171 // デバイスの検出. 172 mBleScanner.startScan(new ScanCallback() { 173 @Override 174 public void onScanResult(int callbackType, ScanResult result) { 175 super.onScanResult(callbackType, result); 176 // スキャン中に見つかったデバイスに接続を試みる.第三引数には接続後に呼ばれるBluetoothGattCallbackを指定する. 177 result.getDevice().connectGatt(getApplicationContext(), false, mGattCallback); 178 } 179 @Override 180 public void onScanFailed(int intErrorCode) 181 { 182 super.onScanFailed(intErrorCode); 183 } 184 }); 185 } 186 public class SendDataTimer extends TimerTask{ 187 @Override 188 public void run() { 189 if(mIsBluetoothEnable) 190 { 191 // 設定時間ごとに0~999までの乱数を作成. 192 mStrSendNum = String.valueOf(mRandom.nextInt(1000)); 193 // UIスレッドで生成した数をTextViewにセット. 194 mBleHandler.sendEmptyMessage(MESSAGE_NEW_SENDNUM); 195 // キャラクタリスティックに値をセットして、Writeリクエストを送信. 196 mBleCharacteristic.setValue(mStrSendNum); 197 mBleGatt.writeCharacteristic(mBleCharacteristic); 198 } 199 } 200 } 201 @Override 202 protected void onDestroy() 203 { 204 // 画面遷移時は通信を切断する. 205 mIsBluetoothEnable = false; 206 if(mBleGatt != null) { 207 mBleGatt.close(); 208 mBleGatt = null; 209 } 210 super.onDestroy(); 211 } 212}
色々質問が多く申し訳ございません。
回答よろしくお願いします。
あなたの回答
tips
プレビュー