@SuppressLint("DefaultLocale")
        @Override
        public void onReceive(Context context, Intent intent) {
          final String action = intent.getAction();
          System.out.println("mGattUpdateReceiver->onReceive->action=" + action);
          if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
            mConnected = true;
            mHandler.removeCallbacks(mConnectingOverTimeRunnable);

          } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
            mConnected = false;
            mConnectionState = connectionStateEnum.isToScan;
            onConectionStateChange(mConnectionState);
            mHandler.removeCallbacks(mDisonnectingOverTimeRunnable);
            mBluetoothLeService.close();
          } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
            // Show all the supported services and characteristics on the user interface.
            for (BluetoothGattService gattService :
                mBluetoothLeService.getSupportedGattServices()) {
              System.out.println(
                  "ACTION_GATT_SERVICES_DISCOVERED  " + gattService.getUuid().toString());
            }
            getGattServices(mBluetoothLeService.getSupportedGattServices());
          } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
            if (mSCharacteristic == mModelNumberCharacteristic) {
              if (intent
                  .getStringExtra(BluetoothLeService.EXTRA_DATA)
                  .toUpperCase()
                  .startsWith("DF BLUNO")) {
                mBluetoothLeService.setCharacteristicNotification(mSCharacteristic, false);
                mSCharacteristic = mCommandCharacteristic;
                mSCharacteristic.setValue(mPassword);
                mBluetoothLeService.writeCharacteristic(mSCharacteristic);
                mSCharacteristic.setValue(mBaudrateBuffer);
                mBluetoothLeService.writeCharacteristic(mSCharacteristic);
                mSCharacteristic = mSerialPortCharacteristic;
                mBluetoothLeService.setCharacteristicNotification(mSCharacteristic, true);
                mConnectionState = connectionStateEnum.isConnected;
                onConectionStateChange(mConnectionState);

              } else {
                Toast.makeText(mainContext, "Please select DFRobot devices", Toast.LENGTH_SHORT)
                    .show();
                mConnectionState = connectionStateEnum.isToScan;
                onConectionStateChange(mConnectionState);
              }
            } else if (mSCharacteristic == mSerialPortCharacteristic) {
              onSerialReceived(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
            }

            System.out.println(
                "displayData " + intent.getStringExtra(BluetoothLeService.EXTRA_DATA));

            //
            //	mPlainProtocol.mReceivedframe.append(intent.getStringExtra(BluetoothLeService.EXTRA_DATA)) ;
            //            	System.out.print("mPlainProtocol.mReceivedframe:");
            //            	System.out.println(mPlainProtocol.mReceivedframe.toString());

          }
        }
Exemplo n.º 2
0
 private boolean sendData(byte[] data) {
   if (target_character != null) {
     target_character.setValue(data);
     return mBluetoothLeService.writeCharacteristic(target_character);
   }
   return false;
 }
 public void sendMessage(View v) {
   final EditText sendEditText = (EditText) findViewById(R.id.send_value);
   String message = sendEditText.getText().toString();
   if (mDataRequestOrCommandCharacteristic == null) {
     Log.e("BluetoothSend", "Cant send " + message);
     return;
   }
   Log.e("BluetoothSend", "Will send " + message);
   mBluetoothLeService.writeCharacteristic(mDataRequestOrCommandCharacteristic, message);
   listenToCaliperChanges();
 }
Exemplo n.º 4
0
 // 发动到ble终端
 public void sendToBle(String cmd) {
   try {
     target_chara.setValue(cmd);
     // 调用蓝牙服务的写特征值方法实现发送数据
     mBluetoothLeService.writeCharacteristic(target_chara);
     //			log("sending to ble : "+cmd);
   } catch (Exception e) {
     log("sendToBle 异常");
     log(e.getMessage());
   }
 }
 public synchronized void sendData(byte[] value) {
   if (targetGattCharacteristic != null && mBluetoothLeService != null && mConnected == true) {
     int targetLen = 0;
     int offset = 0;
     for (int len = (int) value.length; len > 0; len -= 20) {
       if (len < 20) targetLen = len;
       else targetLen = 20;
       byte[] targetByte = new byte[targetLen];
       System.arraycopy(value, offset, targetByte, 0, targetLen);
       offset += 20;
       targetGattCharacteristic.setValue(targetByte);
       mBluetoothLeService.writeCharacteristic(targetGattCharacteristic);
       try {
         Thread.sleep(5);
       } catch (InterruptedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
       }
     }
   }
 }
 public synchronized void sendData(String value) {
   if (targetGattCharacteristic != null && mBluetoothLeService != null && mConnected == true) {
     targetGattCharacteristic.setValue(value);
     mBluetoothLeService.writeCharacteristic(targetGattCharacteristic);
   }
 }