2017-08-31 52 views
0

我遇到问题。我试图从Android智能手机上通过BLE为我的Arduino写入一个值。我的主板是Genuino 101,所以我使用CurieBLE。我能够访问正确的服务和特性,但我总是会得到writeCharacteristic返回的错误值。 onCharacteristicWrite函数也没有被调用。无法使用BLE从Android写入Arduino

这里是我的蓝牙类代码:

public class BluetoothActivity extends AppCompatActivity{ 

private BluetoothAdapter btAdapt = BluetoothAdapter.getDefaultAdapter(); 
private boolean mScan; 
private BluetoothGatt mGatt; 
private Handler handler = new Handler(); 
private static final long SCAN_TIME = 10000; 
private static final String DEVICE_ADDRESS = "98:4F:EE:0F:CB:69"; 
private static final String LIGHT_SERVICE = "19B10000-E8F2-537E-4F6C-D104768A1214"; 
private Context context = this; 
private BluetoothGattCharacteristic sendVal; 

/** 
* Function to scan device 
* @param enable: Switch to begin scanning 
*/ 
public void ScanDevice(final boolean enable){ 
    if(enable){ 
     handler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       mScan = false; 
       btAdapt.stopLeScan(mCallback); 
      } 
     }, SCAN_TIME); 

     mScan = true; 
     btAdapt.startLeScan(mCallback); 
    }else{ 
     mScan = false; 
     btAdapt.stopLeScan(mCallback); 
    } 
} 


/** 
* Callback to execute upon finding device. 
*/ 
private BluetoothAdapter.LeScanCallback mCallback = new BluetoothAdapter.LeScanCallback() { 
    @Override 
    //!Function to connect to device 
    public void onLeScan(final BluetoothDevice device, int i, byte[] bytes) { 
     String address = device.getAddress(); 
     if(address.equals(DEVICE_ADDRESS)) { 
      mGatt = device.connectGatt(context, false, bCallback); 
      Log.i("BTConnect", "Found it!"); 
     } 
     else{ 
      Log.i("BTConnect", "Wrong device!"); 
     } 
    } 
}; 


/** 
* Callback to find services of device. 
*/ 
private final BluetoothGattCallback bCallback = new BluetoothGattCallback() { 

    //!Function to discover services 
    @Override 
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 
     gatt.discoverServices(); 
    } 

    //!Function to deal with discovered services 
    @Override 
    public void onServicesDiscovered(BluetoothGatt gatt, int status) { 
     mGatt = gatt; 
     List<BluetoothGattService> bluetoothGattServiceList= mGatt.getServices(); 
     BluetoothGattService LightUp = bluetoothGattServiceList.get(findService(bluetoothGattServiceList)); 
     List<BluetoothGattCharacteristic> bluetoothGattCharacteristicList = LightUp.getCharacteristics(); 
     sendVal = bluetoothGattCharacteristicList.get(findCharacteristic(bluetoothGattCharacteristicList)); 
    } 

    //!Function to deal with characteristic writes 
    @Override 
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
     if (status == BluetoothGatt.GATT_SUCCESS){ 
      Log.i("BTWrite","Write to Characteristic Success!"); 
     }else{ 
      Log.i("BTWrite","Blast!Foiled!"); 
     } 
    } 
}; 

//!Function to find the right service 
private int findService(List<BluetoothGattService> list){ 
    int index = 0; 
    for(int i = 0; i<list.size(); i++){ 
     if(list.get(i).getUuid().equals(LIGHT_SERVICE)){ 
      index = i;break;} 
    } 
    return index; 
} 

//!Function to find the right characteristic 
private int findCharacteristic(List<BluetoothGattCharacteristic> list){ 
    int index = 0; 
    for(int i = 0; i<list.size(); i++){ 
     if(list.get(i).getUuid().equals(LIGHT_SERVICE)){ 
      index = i;break;} 
    } 
    return index; 
} 

//!Function to actually write to Arduino 
public boolean WriteVal(){ 
    if(sendVal==null){ 
     Log.i("BTWrite", "Disconnected!"); 
     return false; 
    } 
    byte[] value = new byte[1]; 
    value[0] = (byte) (Math.random() * 126); 
    sendVal.setValue(value); 
    boolean ans = mGatt.writeCharacteristic(sendVal); 
    return ans; 
} 

} 

这里是包含UI主类:

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 

private int REQUEST_ENABLE_BT = 1; 
private BluetoothAdapter btAdapt = BluetoothAdapter.getDefaultAdapter(); 
BluetoothActivity btActivity = new BluetoothActivity(); 

/** 
* Main function for opening screen. 
* @param savedInstanceState: Instance for creation 
*/ 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main);//!Open the layout 
    if(btAdapt.isEnabled()){ 
     Intent enableBt = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
     startActivityForResult(enableBt, REQUEST_ENABLE_BT); 
    } 
    findViewById(R.id.button1).setOnClickListener(this); 
    findViewById(R.id.button2).setOnClickListener(this);//!Set listener to button 
} 

@Override 
public void onClick(View v){ 
    switch(v.getId()) { 
     case R.id.button1: 
      btActivity.ScanDevice(true);break;//!Scan for shisha 
     case R.id.button2: 
      btActivity.WriteVal();break;//!Write to shisha 
    } 
} 


} 

我感谢你的帮助。先谢谢你。

回答

0

您的无限do-while循环看起来不太好(为什么它在那里?)。因为这意味着你不会从onServicesDiscovered方法返回,因此你阻止了onCharacteristicWrite回调函数的运行(它被入队)。另请注意,在Android中,每个BluetoothGatt对象可能只有一个未完成的GATT操作(您必须等到相应的回调到达后才能发出其他操作)。这就是writeCharacteristic调用返回false的原因。

+0

即使在do while循环被删除之后,onCharacteristicWrite函数也不会被调用。除此之外,我不执行任何读/写GATT操作。它从一开始就失败了。特征读取成功。 – resonance20