我目前正在使用Arduino Nano 33 BLE从IMU计算角度测量并将这些值添加到服务中的Bluetooth特性中。我使用ArduinoBLE库创建服务和特征:
BLEService angleService("1826");
BLEFloatCharacteristic rollBLE("2A57", BLERead | BLENotify);
在我的设置中,我设置了设备的名称,服务和特征。我最初向特征写入值0:
BLE.setLocalName("acsAssist");
BLE.setAdvertisedService(angleService);
angleService.addCharacteristic(rollBLE);
BLE.addService(angleService);
rollBLE.writeValue(0);
BLE.advertise();
在循环中,在执行计算之后,我相应地更改了特征的值:
rollBLE.writeValue(posiRoll); // posiRoll is my calculation
[当我在Android设备上使用诸如nrfConnect的第三方应用程序时,我可以找到我的设备并连接到它。我定义的服务和特征存在,并且值正在按预期方式更改:
Here is the output from the nrfConnect app
现在,我正在尝试在要构建的Android应用中手动连接到此设备,以便可以在屏幕上显示此更改的值,以供用户查看。我在清单中包含以下所有权限:
<uses-feature android:name="android.hardware.bluetooth" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
我首先检查用户使用的设备是否具有蓝牙适配器,并且该设备是否在蓝牙适配器上:
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) { // Bluetooth not supported
Toast.makeText(getActivity(), "This device does not support Bluetooth! Use a Bluetooth enabled device.", Toast.LENGTH_LONG).show();
}
if (!bluetoothAdapter.isEnabled()) { // If adapter is not enabled, request to enable it from within the app
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
if (bluetoothAdapter.isDiscovering()) {
bluetoothAdapter.cancelDiscovery();
}
由于我知道arduino的MAC地址,因此我尝试使用该MAC地址来获取设备。执行此操作时,我可以查看属性,例如设备名称和地址。但是,当我尝试查看arduino的UUID时,会收到“ null”。以下代码:
BluetoothDevice arduino = bluetoothAdapter.getRemoteDevice("E5:A5:53:32:BD:7C");
Log.i("DEVICES", String.valueOf(arduino.getName()));
Log.i("DEVICES", String.valueOf(arduino.getAddress()));
Log.i("DEVICES", String.valueOf(arduino.getUuids()));
在logcat中产生结果输出:
02-11 12:09:02.862 29503-29503/com.seniorproject.acsAssistApp I/DEVICES: acsAssist
02-11 12:09:02.862 29503-29503/com.seniorproject.acsAssistApp I/DEVICES: E5:A5:53:32:BD:7C
02-11 12:09:02.868 29503-29503/com.seniorproject.acsAssistApp I/DEVICES: null
我一直试图弄清楚如何将我的应用程序连接到arduino并查看我创建的服务,但是我对此却一无所知。我试图找到与此相关的所有指南,问答和文档使我感到绝望和困惑。任何有关如何执行此操作的指导将不胜感激。
PS:如果我提交的格式不正确,我深表歉意。这是我第一次提交有关堆栈溢出的问题。我也是Arduino和Android开发的全新的初学者。
我已经找到解决我难题的答案。我使情况复杂化了;相反,我决定利用实现中的GATT属性。首先,我通过MAC地址获得适配器和arduino。
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice arduino = bluetoothAdapter.getRemoteDevice("E5:A5:53:32:BD:7C");
准备就绪后,连接到arduino:
arduino.connectGatt(this.getActivity(), true, gattCallback);
我实现了我的GATT回调:
private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
final TextView instructionText = getView().findViewById(R.id.instructionText);
Log.i("DEVICES", "Status: " + status);
switch (newState) {
case BluetoothProfile.STATE_CONNECTED:
Log.i("DEVICES", "STATE_CONNECTED");
instructionText.setText("Connected. Select Exercise");
gatt.discoverServices();
break;
case BluetoothProfile.STATE_DISCONNECTED:
Log.i("DEVICES", "STATE_DISCONNECTED");
instructionText.setText("acsAssist Disconnected");
break;
case BluetoothProfile.STATE_CONNECTING:
Log.i("DEVICES", "CONNECTING");
instructionText.setText("Connecting to acsAssist...");
break;
default:
Log.i("DEVICES", "STATE_OTHER");
instructionText.setText("acsAssist Disconnected");
}
}
我执行了我所需的服务发现操作:
@Override
// New services discovered
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.i("DEVICES", "Services Found");
for (BluetoothGattService service: gatt.getServices()) {
Log.i("DEVICES", service.getUuid().toString());
}
for (BluetoothGattCharacteristic characteristic: gatt.getService(UUID.fromString("00001826-0000-1000-8000-00805f9b34fb")).getCharacteristics()) {
gatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
// descriptor.setValue(true ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : new byte[]{0x00, 0x00});
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
}
}
并且我实现了对特征的读取,尤其是当我通过onCharacteristicChanged
对其进行更改时,我可以对其进行监视>
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
//super.onCharacteristicRead(gatt, characteristic, status);
//gatt.setCharacteristicNotification(characteristic, true);
Float tester = characteristic.getFloatValue(BluetoothGattCharacteristic.FORMAT_SFLOAT,1);
Log.i("UUID", String.valueOf(tester));
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
TextView instructionText = getView().findViewById(R.id.instructionText);
instructionText.setText(String.valueOf(characteristic.getFloatValue(BluetoothGattCharacteristic.FORMAT_SFLOAT,1)));
}