HKGalden科技臺
發表文章發起投票
試用 Bluetooth 4.0 駁機,點知 Nexus 得, Samsung 得, HTC 唔得
我用果段 code 行,發現拎緊 characteristics 時候,無啦啦自己 disconnect
點撚算 ?



 
 private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
  @Override
  public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

   if (mBluetoothGatt == null) {
    Log.e(TAG ,  "mBluetoothGatt not created!");
    return;
   }
   BluetoothDevice device = gatt.getDevice();
   String address = device.getAddress();
   try {
    switch (newState) {
    case BluetoothProfile.STATE_CONNECTED:
              connectionState = STATE_CONNECTED;
     broadcastUpdate(ACTION_GATT_CONNECTED, address, status);
     break;
    case BluetoothProfile.STATE_DISCONNECTED:
              connectionState = STATE_DISCONNECTED;
     broadcastUpdate(ACTION_GATT_DISCONNECTED, address, status);
     mBluetoothGatt.connect();

     break;
    default:
     break;
    }
   } catch (NullPointerException e) {
    e.printStackTrace();
   }
  }

  @Override
  public void onServicesDiscovered(BluetoothGatt gatt, int status) {
   BluetoothDevice device = gatt.getDevice();
   if (status == BluetoothGatt.GATT_SUCCESS) {
    
    broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED,  device.getAddress(), status);
   }
  }

  @Override
  public void onCharacteristicChanged(BluetoothGatt gatt,
    BluetoothGattCharacteristic characteristic) {

   broadcastUpdate(ACTION_DATA_NOTIFY, characteristic ,  BluetoothGatt.GATT_SUCCESS);      
  }

  @Override
  public void onCharacteristicRead(BluetoothGatt gatt,
    BluetoothGattCharacteristic characteristic, int status) {

   if (status != BluetoothGatt.GATT_SUCCESS) {
    disconnect();
    return;
   }
   broadcastUpdate(ACTION_DATA_READ, characteristic, status);
  }

  @Override
  public void onCharacteristicWrite(BluetoothGatt gatt,
    BluetoothGattCharacteristic characteristic, int status) {
   try {
    if (status != BluetoothGatt.GATT_SUCCESS) {
     disconnect();     
     return;
    }else{
     if(isCmdCharSet){
      broadcastUpdate(ACTION_DATA_CMD_CHAR, characteristic, status);    
     }else{
      broadcastUpdate(ACTION_DATA_WRITE, characteristic, status); 
     }
      
    }
         
   } catch (Exception e) {
    e.printStackTrace();
   }

  }

  @Override
  public void onDescriptorRead(BluetoothGatt gatt,
    BluetoothGattDescriptor descriptor, int status) {
   Log.i(TAG , "onDescriptorRead");
  }

  @Override
  public void onDescriptorWrite(BluetoothGatt gatt,
    BluetoothGattDescriptor descriptor, int status) {
   Log.i(TAG , "onDescriptorWrite");
  }


 public boolean connect(final String address) {
  if (mBluetoothAdapter == null || address == null) {
   return false;
  }

  if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) {
   if (mBluetoothGatt.connect()) {
                connectionState = STATE_CONNECTING;
    return true;
   } else {
    return false;
   }
  }
  
  final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
  if (device == null) {
   return false;
  }
  mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
  mBluetoothDeviceAddress = address;
        connectionState = STATE_CONNECTING;
  return true;
 }

Good0Bad0
2015/01/27, 4:32:05 下午
本貼文共有 0 個回覆
此貼文已鎖,將不接受回覆
發表文章發起投票