@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BlueToothConstants.BLUETOOTH_ACTION_HANDLE_SERVER.equals(action)) { int handle = intent.getIntExtra(BlueToothConstants.EXTRA_HANDLE_TYPE, 0); switch (handle) { case BlueToothConstants.HANDLE_SERVER_SCAN: // 扫描 SPUtils.setEBikeAddress(BlueToothService.this, ""); // UI发送的重新扫描,则清除保存的设备 SPUtils.setEBikeName(BlueToothService.this, ""); mBluetoothDeviceAddress = ""; mBlueToothConnction.close(); startScanBluetoothDevice(); break; case BlueToothConstants.HANDLE_SERVER_CONNECT: // 链接 mBluetoothDeviceAddress = intent.getStringExtra(BlueToothConstants.EXTRA_DATA); connectDevice(mBluetoothDeviceAddress); break; case BlueToothConstants.HANDLE_SERVER_DISCONNECT: // 断开链接 SPUtils.setEBikeAddress(BlueToothService.this, ""); // UI发送的重新扫描,则清除保存的设备 SPUtils.setEBikeName(BlueToothService.this, ""); disconnect(); break; case BlueToothConstants.HANDLE_SERVER_SYNC: // 同步 // 默认只有链接上蓝牙才去同步 if (mBlueToothConnction.getState() == BluetoothConnection.STATE_CONNECTED) { syncHistory(); // mark 如果想同步历史记录,则打开此处 } else { // 没有连接就去扫描,并连接 startScanBluetoothDevice(); } break; case BlueToothConstants.HANDLE_SERVER_SEND_DATA: // 发送数据 // simulate(); Gson gson = new Gson(); String str = intent.getStringExtra(BlueToothConstants.EXTRA_DATA); byte[] data = gson.fromJson(str, byte[].class); if (data != null) { if (data.length > 0) { int cmd = data[0]; byte[] sendData = new byte[data.length - 1]; for (int i = 0; i < sendData.length; i++) { sendData[i] = data[i + 1]; } if (mBlueToothConnction != null && mReceiveCharacteristic != null) { // 开启数据监听 LogUtils.i(tag, "监听数据"); mBlueToothConnction.setCharacteristicNotification( mReceiveCharacteristic, true); } // mark 加上这个,是因为,如果一设置好监听就发送请求数据包,将得不到数据,所以要做一个延迟处理,设置监听后500毫秒再发送请求数据包 Message msg = Message.obtain(); msg.what = cmd; msg.obj = sendData; sendDataControl.sendMessageDelayed(msg, 500); } } break; default: break; } } }
@Override public int onStartCommand(Intent intent, int flags, int startId) { mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); mBluetoothDeviceAddress = SPUtils.getEBkieAddress(this); // 设备地址 if (mBlueToothConnction == null) { mBlueToothConnction = new BluetoothConnection(this, mHandler); } boolean isGoon = false; if (mReConnectThread == null) { mReConnectThread = new ReConnectThread(); mReConnectThread.start(); } if (mBlueToothConnction.getState() != BluetoothConnection.STATE_CONNECTED) { // 只要不是在链接的情况下就要重新链接 isGoon = true; sendBleState(BlueToothConstants.BLE_STATE_DISCONNECTED); } else { // 已连接 sendBleState(BlueToothConstants.BLE_STATE_CONNECTED); } LogUtils.i(tag, "isGoon" + isGoon); if (isGoon && stateEnableStep()) { // 客户端自动链接 if (!TextUtils.isEmpty(mBluetoothDeviceAddress)) { sendBleState(BlueToothConstants.BLE_STATE_CONNECTTING); startScanBluetoothDevice(); } else { sendBleState(BlueToothConstants.BLE_STATE_NONE); } } return super.onStartCommand(intent, flags, startId); }
@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; } }