/** * Sends a message. * * @param message A string of text to send. */ private synchronized void sendData(int commandCode, byte[] data) { byte[] send = ProtocolByteHandler.command(commandCode, data); LogUtils.i(tag, "发送出去的数据包16进制为:" + ProtocolTool.bytesToHexString(send)); // Check that we're actually connected before trying anything if (mBlueToothConnction.getState() != BluetoothConnection.STATE_CONNECTED) { toastMessage(getString(R.string.bluetooth_not_connect)); return; } if (send != null) { // Get the message bytes and tell the BluetoothChatService to write if (mSendCharacteristic != null) { mBlueToothConnction.sendData(send, mSendCharacteristic); } else { printlnMessage("发送数据的特征值为空!"); } } }
@Override public void handleMessage(Message msg) { switch (msg.what) { case BluetoothConnection.ACTION_GATT_CONNECTED: printlnMessage("链接上蓝牙服务"); SPUtils.setEBikeAddress(getApplicationContext(), mBluetoothDeviceAddress); if (mBlueToothConnction != null && mReceiveCharacteristic != null) { // 开启数据监听 LogUtils.i(tag, "监听数据"); mBlueToothConnction.setCharacteristicNotification(mReceiveCharacteristic, true); } sendBleState(BlueToothConstants.BLE_STATE_CONNECTED); break; case BluetoothConnection.ACTION_DATA_AVAILABLE: byte[] receiveData = (byte[]) msg.obj; // byte[] // receiveData={(byte)0xFE,(byte)0xfe,(byte)0x30,(byte)0x0e,(byte)0x00,(byte)0x01,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x25,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x5a,(byte)0xa7,(byte)0xbb}; // byte[] // receiveData={(byte)0xfe,(byte)0xfe,(byte)0x30,(byte)0x0c,(byte)0x45,(byte)0x4f,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x01,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x32,(byte)0x19,(byte)0x87,(byte)0xec,(byte)0xbb}; printlnMessage("收到数据:" + ProtocolTool.bytesToHexString(receiveData)); HashMap<String, Object> map = ProtocolByteHandler.parseData(BlueToothService.this, receiveData); if (map != null) { // 说明本次接收到的数据没有问题 // 历史记录不用发送到UI上面 int cmd = (int) map.get(ProtocolByteHandler.EXTRA_CMD); if (cmd != CommandCode.HISTORY) { broadCastData2UI( BlueToothConstants.BLUETOOTH_ACTION_HANDLE_SERVER_RESULT_SEND_DATA, BlueToothConstants.RESULT_SUCCESS, null); // 提示UI更新 //mark 现改成,发送数据的时候就提示UI更新,因为需要流畅的时间显示 } // if(!isRequestData){//由于最后一次暂停 了,可能是没有发送出去。所以要把最后一次更新到UI上 // // broadCastData2UI(BlueToothConstants.BLUETOOTH_ACTION_HANDLE_SERVER_RESULT_SEND_DATA,BlueToothConstants.RESULT_SUCCESS,null); // }//mark // 再次打开上面的广播UI,是因为如果是手动发送数据,就会出现,这次数据返回前,先更新UI,而数据回到这里后,没有更新UI,之前考虑到会耗性能,不过现在觉得一秒内发2,到3次广播,也是可以的。 } break; case BluetoothConnection.ACTION_GATT_DISCONNECTED: if (mRequestDataThread != null) { mRequestDataThread.cancel(); } printlnMessage("蓝牙服务链接断开"); // 保存当前断开的时间 SPUtils.setEbikeDisconnectTime(BlueToothService.this); sendBleState(BlueToothConstants.BLE_STATE_DISCONNECTED); break; case BluetoothConnection.ACTION_GATT_SERVICES_DISCOVERED: printlnMessage("蓝牙服务发现完毕"); mReceiveCharacteristic = null; mSendCharacteristic = null; List<BluetoothGattService> gattServices = mBlueToothConnction.getSupportedGattServices(); String uuid = ""; // 特征的uuid // Loops through available GATT Services. for (BluetoothGattService gattService : gattServices) { uuid = gattService.getUuid().toString(); List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics(); if (uuid.contains("ffe0")) { // 监听数据服务 for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) { uuid = gattCharacteristic.getUuid().toString(); if (uuid.contains("ffe4")) { // 服务"ffe0"下的"ffe4"特征为打开监听数据特征 printlnMessage("找到接收数据的特征值了--ffe4"); mReceiveCharacteristic = gattCharacteristic; } } } else if (uuid.contains("ffe5")) { // 发送数据服务 for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) { uuid = gattCharacteristic.getUuid().toString(); if (uuid.contains("ffe9")) { // 服务"ffe5"下的"ffe9"特征为发送特征 printlnMessage("找到发送数据的特征值了--ffe9"); mSendCharacteristic = gattCharacteristic; } } } } if (mReceiveCharacteristic != null && mSendCharacteristic != null) { // 初始化完毕,可以启动心跳包,发送数据了 LogUtils.i(tag, "服务特征值已找到," + BaseApplication.travelState); if (BaseApplication.travelState == TravelConstant.TRAVEL_STATE_START || BaseApplication.travelState == TravelConstant.TRAVEL_STATE_RESUME || BaseApplication.travelState == TravelConstant.TRAVEL_STATE_FAKE_PAUSE) { // 说明在运行中 startTravel(); } else if (BaseApplication.travelState != TravelConstant.TRAVEL_STATE_PAUSE) { syncHistory(); // mark 同步历史数据 } } else { mBlueToothConnction.setState(BluetoothConnection.STATE_DISCONNECTED); toastMessage(getString(R.string.bluetooth_service_init_fail)); toastMessage(getString(R.string.bluetooth_service_re_init)); isAppInitBluetooth = true; mBluetoothAdapter.disable(); sendBleState(BlueToothConstants.BLE_STATE_DISCONNECTED); } break; default: break; } }