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
}
}
}
我感謝你的幫助。先謝謝你。
即使在do while循環被刪除之後,onCharacteristicWrite函數也不會被調用。除此之外,我不執行任何讀/寫GATT操作。它從一開始就失敗了。特徵讀取成功。 – resonance20