现在我知道这个问题已经被问了十亿次了。
我已经阅读了其中的大部分内容(这个网站对我的问题没有太大帮助),我已经阅读了文档,并且我已经阅读了示例和教程。
我想做的是:
向 BLE 设备发送 hello world(连接到 arduino)
到目前为止我所做的:
使用内置设置与设备配对(我的应用程序中没有编程逻辑)
使用 GATT 服务器与设备连接。
但是,当我尝试编写特征时,相应的状态返回布尔值始终为 false。这是最相关的代码:
private void setupServer(BluetoothAdapter bAdapter) {
Log.d(TAG, "Started setting up server");
BluetoothLeAdvertiser bAdvertiser = bAdapter.getBluetoothLeAdvertiser();
if (bAdapter != null) {
bGattServer = bManager.openGattServer(getApplicationContext(), new BluetoothGattServerCallback() {
@Override
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
super.onConnectionStateChange(device, status, newState);
}
});
//Create a service using a random UUID
//Create characteristics with WRITE property and write a value
BluetoothGattService service = new BluetoothGattService(UUID.randomUUID(), BluetoothGattService.SERVICE_TYPE_PRIMARY);
BluetoothGattCharacteristic bluetoothGattCharacteristic = new BluetoothGattCharacteristic(bUUID, BluetoothGattCharacteristic.PROPERTY_WRITE, BluetoothGattCharacteristic.PERMISSION_WRITE);
Log.d(TAG, "Adding characteristic to service " + service.addCharacteristic(bluetoothGattCharacteristic));
bluetoothGattCharacteristic.setValue("hello");
Log.d(TAG, "Trying to write characteristic..., first value: " + bluetoothGattCharacteristic.getValue()[0]);
if (bGatt != null) {
boolean success = bGatt.writeCharacteristic(bluetoothGattCharacteristic);
if (success) {
Log.d(TAG, "Successfuly wrote characteristic to" + bUUID);
} else {
//THIS ALWAYS EXECUTES??
Log.e(TAG, "Failed to write to characteristic");
}
} else {
Log.e(TAG, "BGATT IS NULL");
}
service.addCharacteristic(bluetoothGattCharacteristic);
bGatt.setCharacteristicNotification(bluetoothGattCharacteristic, true);
bGattServer.addService(service);
}
这些是我的权限:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-feature android:name="android.hardware.bluetooth_le" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
这是相关代码的其余部分
private boolean scanBluetooth(final BluetoothAdapter bAdapter, final BluetoothDevice[] bDevice) {
final BluetoothLeScanner bScanner = bAdapter.getBluetoothLeScanner();
final boolean[] stopCallback = {false};
Log.d(TAG,"Started scanning");
bScanner.startScan(new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
bDevice[0] = result.getDevice();
if (bDevice[0] == null || stopCallback[0]) {
Toast.makeText(getApplicationContext(), "Didn't find a device", Toast.LENGTH_LONG);
} else {
stopCallback[0] = true;
System.out.println("DEVICE" + bDevice[0]);
bGatt = bDevice[0].connectGatt(getApplicationContext(), true, new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if(status == BluetoothGatt.GATT_SUCCESS){
Log.d(TAG, "Successful GATT " + newState);
setupServer(bAdapter);
return;
}
if(status == BluetoothGatt.GATT_FAILURE){
Log.e(TAG,"Bigg error boi");
System.exit(1);
}
else{
Log.w(TAG,"Oh no, gatt failed " + status + " retrying");
return;
}
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
Log.d(TAG, "Characteristic written" + status);
}
}, BluetoothDevice.TRANSPORT_LE);
Log.d(TAG, "Bgatt value:" + bGatt);
}
}
});
if(bGatt==null){
Log.w(TAG, "BGATT IS NULL WHILE SCANNING");
}
Log.d(TAG, "Finished scanning...");
return true;
}
我已经使用应用程序 BLEScanner 获得了 bUUID,它显示了一个用于读写的 UUID。我的应用程序的目标是反映该应用程序的功能,但我开始失去理智。我什至还不能发送一个简单的hello world。
Android Studio 输出:
018-12-01 23:58:56.214 25024-25136/com.iopt.davidv7.iopt W/DBG: Oh no, gatt failed 22 retrying
2018-12-01 23:58:56.675 25024-25136/com.iopt.davidv7.iopt D/DBG: Successful GATT 2
2018-12-01 23:58:56.675 25024-25136/com.iopt.davidv7.iopt D/DBG: Started setting up server
2018-12-01 23:58:56.692 25024-25136/com.iopt.davidv7.iopt D/DBG: Adding characteristic to service true
2018-12-01 23:58:56.693 25024-25136/com.iopt.davidv7.iopt D/DBG: Trying to write characteristic..., first value: 104
2018-12-01 23:58:56.693 25024-25136/com.iopt.davidv7.iopt E/DBG: Failed to write to characteristic
客户端是一个简单的 HM-10 蓝牙模块,我现在想使用 ARDUINO IDE 将我在应用程序中发送的内容镜像到串行监视器上。
我希望我已经给了你所有的背景,因为我正在失去希望。我没有手动检查权限,因为我已经使用系统对话框设置了权限,但是,只有“位置”可以启用?这可能是我问题的根源吗?用 checkSelfPermission 检查蓝牙,蓝牙管理员返回 true,所以我不认为是这样:
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.BLUETOOTH_ADMIN)
!= PackageManager.PERMISSION_GRANTED) {
Log.e(TAG,"No permission for bluetooth");
// Permission is not granted
}
else{
Log.d(TAG,"Bluetooth permission granted");
}