/** 收到BLE终端写入数据回调 */ @Override public void onCharacteristicWrite( BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { Log.e( TAG, "onCharWrite " + gatt.getDevice().getName() + " write " + characteristic.getUuid().toString() + " -> " + Utils.bytesToHexString(characteristic.getValue())); switch (characteristic.getValue()[0]) { case (byte) 0x55: // 实时体重数据 appState.calcWeight(characteristic.getValue()); // 解析实时体重 appState.dataArrive = true; break; case (byte) 0x5A: // 完整测量结果数据 appState.calcAllWeight(characteristic.getValue()); // 解析实时体重 appState.dataArrive = true; break; default: break; } }
/** BLE终端数据被读的事件 */ @Override public void onCharacteristicRead( BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (status == BluetoothGatt.GATT_SUCCESS) Log.e( TAG, "onCharRead " + gatt.getDevice().getName() + " read " + characteristic.getUuid().toString() + " -> " + Utils.bytesToHexString(characteristic.getValue())); }
private void displayGattServices(List<BluetoothGattService> gattServices) { if (gattServices == null) return; for (BluetoothGattService gattService : gattServices) { // -----Service的字段信息-----// int type = gattService.getType(); Log.e(TAG, "-->service type:" + Utils.getServiceType(type)); Log.e(TAG, "-->includedServices size:" + gattService.getIncludedServices().size()); Log.e(TAG, "-->service uuid:" + gattService.getUuid()); // -----Characteristics的字段信息-----// List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics(); for (final BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) { Log.e(TAG, "---->char uuid:" + gattCharacteristic.getUuid()); int permission = gattCharacteristic.getPermissions(); Log.e(TAG, "---->char permission:" + Utils.getCharPermission(permission)); int property = gattCharacteristic.getProperties(); Log.e(TAG, "---->char property:" + Utils.getCharPropertie(property)); byte[] data = gattCharacteristic.getValue(); if (data != null && data.length > 0) { Log.e(TAG, "---->char value:" + Utils.bytesToHexString(data)); } if (gattService.getUuid().toString().equals(UUID_KEY_SERVICE)) { // 如果是温度计的service // UUID_KEY_DATA是可以跟蓝牙模块串口通信的Characteristic if (gattCharacteristic.getUuid().toString().equals(UUID_KEY_DATA_RECIV)) { // 测试读取当前Characteristic数据,会触发mOnDataAvailable.onCharacteristicRead() mHandler.postDelayed( new Runnable() { @Override public void run() { mBLE_reciv.readCharacteristic(gattCharacteristic); } }, 500); // 接受Characteristic被写的通知,收到蓝牙模块的数据后会触发mOnDataAvailable.onCharacteristicWrite() mBLE_reciv.setCharacteristicNotification(gattCharacteristic, true); BluetoothGattDescriptor localBluetoothGattDescriptor = gattCharacteristic.getDescriptor(UUID.fromString(CCC)); localBluetoothGattDescriptor.setValue( BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(localBluetoothGattDescriptor); appState.gattCharacteristic_reciv_dianzichen = gattCharacteristic; // 设置数据内容 // gattCharacteristic.setValue("0"); // 往蓝牙模块写入数据 // appState.mBLE_reciv_dianzichen.writeCharacteristic(gattCharacteristic); } // UUID_KEY_DATA是可以跟蓝牙模块串口通信的Characteristic if (gattCharacteristic.getUuid().toString().equals(UUID_KEY_DATA_SEND)) { // 测试读取当前Characteristic数据,会触发mOnDataAvailable.onCharacteristicRead() mHandler.postDelayed( new Runnable() { @Override public void run() { mBLE_send.readCharacteristic(gattCharacteristic); // Log.i("info", "appState.mBLE_send_dianzichen 尝试读数据"); } }, 500); // 接受Characteristic被写的通知,收到蓝牙模块的数据后会触发mOnDataAvailable.onCharacteristicWrite() mBLE_send.setCharacteristicNotification(gattCharacteristic, true); // 设置数据内容 // gattCharacteristic.setValue("send data->"); // 往蓝牙模块写入数据 // mBLE_send.writeCharacteristic(gattCharacteristic); if (!appState.firstActivityRunning) { appState.gattCharacteristic_send_dianzichen = gattCharacteristic; appState.file.write2SDFromInput("inurse/", "Dianzichen.txt", appState.deviceAddress); Intent it = new Intent(this, DianzichenActivity.class); startActivityForResult(it, 0); // 配合onActivityResult,当FirstActivity退出时获得一个0值,然后自己也退出 // moveTaskToBack(true); //这一句是整个程序返回桌面 appState.firstActivityRunning = true; } } } // 结束 if ( gattService.getUuid().toString().equals(UUID_KEY_SERVICE) ) // -----Descriptors的字段信息-----// List<BluetoothGattDescriptor> gattDescriptors = gattCharacteristic.getDescriptors(); for (BluetoothGattDescriptor gattDescriptor : gattDescriptors) { Log.e(TAG, "-------->desc uuid:" + gattDescriptor.getUuid()); int descPermission = gattDescriptor.getPermissions(); Log.e(TAG, "-------->desc permission:" + Utils.getDescPermission(descPermission)); byte[] desData = gattDescriptor.getValue(); if (desData != null && desData.length > 0) { Log.e(TAG, "-------->desc value:" + Utils.bytesToHexString(data)); } // soloman 接收的uuid要设置CCC通知enable,并回写到BluetoothGatt // if (gattDescriptor.getUuid().toString().equals(CCC)){ // gattDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); // mBluetoothGatt.writeDescriptor(gattDescriptor); // } } } } // }