我仅在Oneplus设备上有问题,诺基亚在Pixel上可以很好地工作。
这是我的问题。我有BLE设备,并使用以下代码通过我的应用程序连接到它。
public boolean connect(final String address) {
if (mBluetoothAdapter == null || address == null) {
Log.e(TAG, "BluetoothAdapter not initialized or unspecified address.");
return false;
}
String mBluetoothDeviceAddress = Util.getDeviceAddress(this);
// Previously connected device. Try to reconnect.
if (mBluetoothGatt != null && Util.isBLEDeviceConnected(this, mBluetoothDeviceAddress)) {
Log.d(TAG, "Already connected");
broadcastUpdate(ACTION_GATT_CONNECTED, mBluetoothDeviceAddress);
return true;
} else if (mBluetoothGatt != null) {
Log.d(TAG, "GATT Connection is Required");
mBluetoothGatt.connect();
return true;
}
if (!mIsConnecting) {
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
if (device == null) {
Log.w(TAG, "Device not found. Unable to connect.");
return false;
}
// We want to auto connect to the device, so we are setting the autoConnect
// parameter to true.
Log.d(TAG, "Trying to create a new connection.");
mBluetoothGatt = device.connectGatt(this, true, mGattCallback);
mIsConnecting = true;
} else {
Log.d(TAG, "Already connection is in progress");
}
return true;
}
@Override
public void onDestroy() {
super.onDestroy();
disconnect();
close();
unregisterReceiver(bluetoothReceiver);
unregisterReceiver(mGattDisconnectReceiver);
}
public void disconnect() {
Log.d(TAG, "Disconnecting Gatt " + mBluetoothGatt);
if (mBluetoothGatt == null) {
Log.w(TAG, "BluetoothGatt not initialized");
return;
}
mBluetoothGatt.disconnect();
mIsConnecting = false;
}
public void close() {
Log.d(TAG, "Closing Gatt " + mBluetoothGatt);
if (mBluetoothGatt == null) {
Log.w(TAG, "BluetoothGatt not initialized");
return;
}
mBluetoothGatt.close();
mIsConnecting = false;
mBluetoothGatt = null;
}
配对正常,直到我关闭/关闭手机。如果我下次尝试配对/连接时重新启动手机,它将无法正常工作。它按照我的代码说“正在尝试建立新的连接”,并在下面附上日志。
2019-03-01 09:01:08.083 9983-9983/com.wrkspot.employee.dev D/BLEService: Trying to create a new connection.
2019-03-01 09:01:08.084 9983-9983/com.wrkspot.employee.dev D/BluetoothGatt: connect() - device: C1:B4:70:12:4B:23, auto: true
2019-03-01 09:01:08.084 9983-9983/com.wrkspot.employee.dev D/BluetoothGatt: registerApp()
2019-03-01 09:01:08.085 9983-9983/com.wrkspot.employee.dev D/BluetoothGatt: registerApp() - UUID=a18ee742-4543-473c-8789-37a22845a96c
2019-03-01 09:01:08.087 9983-10064/com.wrkspot.employee.dev D/BluetoothGatt: onClientRegistered() - status=0 clientIf=8
我被困住了,我真的需要一些建议。此问题仅在我的Oneplus 3T上出现,而在我测试过的其他几部手机中,它工作正常。
发生此问题时,我必须重新安装该应用程序并重新启动手机。只有这样,我才能再次配对/连接。
答案 0 :(得分:2)
值为true的自动连接仅在Android设备具有其缓存或绑定了设备时才有效。重新启动手机后,缓存将被清除。因此,您必须再次扫描....