android - 如何不丢失连接BLE
问题描述
我正在尝试在 TI 设备和我的应用程序之间进行 BLE 通信。此时,不启用配对,一切正常,一旦启用配对,我就有问题。
1)我的应用程序要求我配对。
2) 使用密码进入
3) APP 不工作
4) 我离开APP,返回,它工作
5) 我从APP 重新退出,重新进入不工作,需要配对。
如何解决这些错误?
我在 MainActivity 中使用它:
private void getpaireddevices(){
Set<BluetoothDevice> devicesArray = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
if(devicesArray.size() > 0) {
for(BluetoothDevice device : devicesArray) {
device.getName();
device.getAddress();
}
}
}
这在 SelectedDevice 中:
private final BroadcastReceiver mBroadcastReceiver4 = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);
int prevstate = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1);
String msg = "Bond state change: state" + pairstate(state) + "previous state" + pairstate(prevstate);
Log.w("Bond state receiver", msg);
}
private String pairstate(int state) {
switch (state) {
case BluetoothDevice.BOND_BONDING:
Log.i("Bondind status:", "Bonding..");
break;
case BluetoothDevice.BOND_BONDED:
Log.i("Bondind status:", "Bonded");
break;
case BluetoothDevice.BOND_NONE:
Log.i("Bondind status:", "Fail");
default:
return String.valueOf(state);
}
return null;
}
};
在扫描设备时的主要活动中,我调用了createBond ()
SelectedDevice 中的mBroadcastReceiver4
.
解决方案
与 BLE 设备交互的第一步是连接到它——更具体地说,连接到设备上的 GATT 服务器。
bluetoothGatt = device.connectGatt(context, false, gattCallback);
private final BluetoothGattCallback gattCallback =
new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status,
int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
connectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server.");
Log.i(TAG, "Attempting to start service discovery:" +
bluetoothGatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
connectionState = STATE_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction);
} else {
Log.w(TAG, "onConnectionStateChange() status = " + status + " newState = " + newState);
}
}
@Override
// New services discovered
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
@Override
// Result of a characteristic read operation
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
}
}
当您尝试连接到您的设备时,您在 onConnectionStateChange() 中看到了什么日志?
您是否在您的设备上发现了任何服务?
有关连接到 BLE 设备的更多信息: https ://developer.android.com/guide/topics/connectivity/bluetooth-le.html
推荐阅读
- node.js - aws nodejs sdk putObjectAcl 正确语法
- sass - OroCommerce 4.1 默认主题自定义
- typescript - 尝试使用 Typescript 访问“firebase.firestore.Timestamp”。错误:类型“()=> Firestore”上不存在属性“时间戳”
- ksqldb - 如何实现 KSQLDB 初始化脚本?
- sqlite - SQLite - FOREIGN KEY 不起作用(是的,它已启用)
- javascript - 如何使用 fs.readFileSync 获得德语变音符号?
- php - 如何查找多个 ID
- javascript - dc.js v4.1.0 和 d3 v6.1.1:未捕获的类型错误:无法读取未定义的属性“2”;d 是鼠标事件
- python - 清理亚马逊链接的正则表达式
- c# - 我如何发送消息选项卡和向上箭头发送到 winform?C#