分享Android 蓝牙4.0(ble)开发的解决方案

2019-12-10 18:47:32于海丽

2.8获取到特征之后,找到服务中可以向下位机写指令的特征,向该特征写入指令。

public void wirteCharacteristic(BluetoothGattCharacteristic characteristic) { 
 
  if (mBluetoothAdapter == null || mBluetoothGatt == null) { 
   Log.w(TAG, "BluetoothAdapter not initialized"); 
   return; 
  } 
 
  mBluetoothGatt.writeCharacteristic(characteristic); 
 
 } 

2.9写入成功之后,开始读取设备返回来的数据。

 

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { 
  @Override 
  public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 
   String intentAction; 
   //System.out.println("=======status:" + status); 
   if (newState == BluetoothProfile.STATE_CONNECTED) { 
    intentAction = ACTION_GATT_CONNECTED; 
    mConnectionState = STATE_CONNECTED; 
    broadcastUpdate(intentAction); 
    Log.i(TAG, "Connected to GATT server."); 
    // Attempts to discover services after successful connection. 
    Log.i(TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices()); 
 
   } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { 
    intentAction = ACTION_GATT_DISCONNECTED; 
    mConnectionState = STATE_DISCONNECTED; 
    Log.i(TAG, "Disconnected from GATT server."); 
    broadcastUpdate(intentAction); 
   } 
  } 
 
  @Override 
  public void onServicesDiscovered(BluetoothGatt gatt, int status) { 
   if (status == BluetoothGatt.GATT_SUCCESS) { 
    broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED); 
   } else { 
    Log.w(TAG, "onServicesDiscovered received: " + status); 
   } 
  } 
  //从特征中读取数据 
  @Override 
  public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
   //System.out.println("onCharacteristicRead"); 
   if (status == BluetoothGatt.GATT_SUCCESS) { 
    broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); 
   } 
  } 
  //向特征中写入数据 
  @Override 
  public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
   //System.out.println("--------write success----- status:" + status); 
  } 
 
  /* 
   * when connected successfully will callback this method this method can 
   * dealwith send password or data analyze 
   
   *当连接成功将回调该方法 
   */ 
  @Override 
  public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { 
   broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); 
   if (characteristic.getValue() != null) { 
 
    //System.out.println(characteristic.getStringValue(0)); 
   } 
   //System.out.println("--------onCharacteristicChanged-----"); 
  } 
 
  @Override 
  public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { 
 
   //System.out.println("onDescriptorWriteonDescriptorWrite = " + status + ", descriptor =" + descriptor.getUuid().toString()); 
 
   UUID uuid = descriptor.getCharacteristic().getUuid(); 
   if (uuid.equals(UUID.fromString("0000cd01-0000-1000-8000-00805f9b34fb"))) { 
    broadcastUpdate(ACTION_CD01NOTIDIED); 
   } else if (uuid.equals(UUID.fromString("0000cd02-0000-1000-8000-00805f9b34fb"))) { 
    broadcastUpdate(ACTION_CD02NOTIDIED); 
   } else if (uuid.equals(UUID.fromString("0000cd03-0000-1000-8000-00805f9b34fb"))) { 
    broadcastUpdate(ACTION_CD03NOTIDIED); 
   } else if (uuid.equals(UUID.fromString("0000cd04-0000-1000-8000-00805f9b34fb"))) { 
    broadcastUpdate(ACTION_CD04NOTIDIED); 
   } 
  } 
 
  @Override 
  public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) { 
   //System.out.println("rssi = " + rssi); 
  } 
 }; 
  
 ---------------------------------------------- 
  //从特征中读取数据 
  @Override 
  public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
   //System.out.println("onCharacteristicRead"); 
   if (status == BluetoothGatt.GATT_SUCCESS) { 
    broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); 
   } 
  }