无法使用BLE从Android写入Arduino

时间:2017-08-31 17:59:43

标签: android bluetooth arduino bluetooth-lowenergy

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

以下是我的蓝牙课程的代码:

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
    }
}


}

感谢你的帮助。提前谢谢。

1 个答案:

答案 0 :(得分:0)

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