nicht möglich Ich habe ein Problem. Ich versuche, einen Wert auf meinem Arduino von meinem Android-Smartphone über BLE zu schreiben. Mein Board ist ein Genuino 101, also benutze ich CurieBLE. Ich kann auf den richtigen Dienst und das richtige Merkmal zugreifen, erhalte aber immer einen falschen Wert, der von writeCharacteristic zurückgegeben wird. Die onCharacteristicWrite-Funktion wird auch nicht aufgerufen. HierSchreiben von Arduino von Android mit BLE
ist der Code für die Bluetooth-Klasse:
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;
}
}
Und hier ist die Hauptklasse, die die Benutzeroberfläche enthält:
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
}
}
}
Ich schätze Ihre Hilfe. Vielen Dank im Voraus.
Auch nachdem die do while-Schleife entfernt wurde, wird die onCharacteristicWrite-Funktion nicht aufgerufen. Ich führe keine andere GATT-Lese-/Schreiboperation als diese aus. Es scheitert von Anfang an. Das Lesen der Merkmale ist erfolgreich. – resonance20