@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); String sendData = ""; if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) { // 蓝牙状态 if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0) == BluetoothAdapter.STATE_ON) { sendData = "蓝牙打开"; printlnMessage(sendData); startScanBluetoothDevice(); } else if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0) == BluetoothAdapter.STATE_OFF) { sendData = "蓝牙关闭"; printlnMessage(sendData); if (isAppInitBluetooth) { // 如果是应用自动关闭蓝牙进行初始化,则在关闭状态触发后,再次打开 isAppInitBluetooth = false; mBluetoothAdapter.enable(); } else { sendBleState(BlueToothConstants.BLE_STATE_DISCONNECTED); } } } else if (ACTION_SMS.equals(action)) { // 短信 sendData = "接收到短信"; printlnMessage(sendData); EBikeStatus.getInstance(context) .setBikeStatus(EBikeStatus.RECEIVE_MESSAGE, EBConstant.ON); } else if (ACTION_PHONE.equals(action)) { // 电话 sendData = "接收到电话"; printlnMessage(sendData); TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE); switch (tm.getCallState()) { case TelephonyManager.CALL_STATE_RINGING: printlnMessage("来电"); EBikeStatus.getInstance(context) .setBikeStatus(EBikeStatus.PHONE_CALL, EBConstant.ON); break; case TelephonyManager.CALL_STATE_OFFHOOK: printlnMessage("挂机"); EBikeStatus.getInstance(context) .setBikeStatus(EBikeStatus.PHONE_CALL, EBConstant.OFF); break; case TelephonyManager.CALL_STATE_IDLE: printlnMessage("拨打"); EBikeStatus.getInstance(context) .setBikeStatus(EBikeStatus.PHONE_CALL, EBConstant.OFF); break; } } }
public void run() { LogUtils.i(tag, "BEGIN RequestDataThread:"); if (mBlueToothConnction.getState() != BluetoothConnection.STATE_CONNECTED) { toastMessage(getString(R.string.bluetooth_not_connect)); connectDevice(mBluetoothDeviceAddress); // state=TravelConstant.TRAVEL_STATE_PAUSE; // BaseApplication.sendStateChangeBroadCast(BlueToothService.this, // state, false, mStateReceiver); return; } setName("RequestDataThread"); while (BaseApplication.workModel == EBConstant.WORK_BLUETOOTH && isRequestDataRunning && BluetoothConnection.STATE_CONNECTED == mBlueToothConnction.getState()) { printlnMessage( "要发送出去的数据进进制值是:" + EBikeStatus.getInstance(getApplicationContext()).getBikeStatus() + " 格式化8位是:" + ProtocolTool.byteToBitString( new byte[] {EBikeStatus.getInstance(getApplicationContext()).getBikeStatus()})); if (isRequestData) { // 如果是暂停,就不需要再发送获取数据的命令了 broadCastData2UI( BlueToothConstants.BLUETOOTH_ACTION_HANDLE_SERVER_RESULT_SEND_DATA, BlueToothConstants.RESULT_SUCCESS, null); sendData( CommandCode.SURVEY, new byte[] { EBikeStatus.getInstance(getApplicationContext()).getBikeStatus() }); // 获取蓝牙数据 } try { Thread.sleep(REQUESTDATA_SPACING); } catch (InterruptedException e) { LogUtils.e(tag, e.getMessage()); } } // Reset the ConnectThread because we're done synchronized (BlueToothService.this) { mRequestDataThread = null; } }