- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有一个 Android 应用程序,它使用 蓝牙低功耗 (BLE) 与我的手机连接。该应用程序在几乎所有手机上运行良好,例如 Galaxy S5 (Android 5.0.1)、Galaxy S6 (Android 6.0)。但是,它在 Galaxy Note 3 (Android 5.0.1) 中存在严重问题。
当我调用 connect()
函数,然后调用 readCharacteristic()
时,它返回错误:
onClientConnectionState() - status=22 clientIf=7
在我调用 connect
函数大约 2 秒后发生错误。我查了一些解决方法,比如在调用 readCharacteristic()
之前做一个延迟,但是不能解决。
您有什么办法可以解决我的问题吗?
谢谢大家。
这是我的代码:
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
mBluetoothGatt.discoverServices();
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
try {
Thread.sleep(500);
readCharacteristic();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
}
};
public void readCharacteristic() {
/*check if the service is available on the device*/
BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("00001c00-d102-11e1-9b23-00025b00123"));
/*get the read characteristic from the service*/
BluetoothGattCharacteristic mReadCharacteristic = mCustomService.getCharacteristic(UUID.fromString("00001233-d102-11e1-9b23-00025b00a5a5"));
mBluetoothGatt.setCharacteristicNotification(mReadCharacteristic, true);
BluetoothGattDescriptor descriptor = mReadCharacteristic.getDescriptor(
UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
这是我连接BLE的完整服务功能文件
public class ConnectionService extends Service{
private BluetoothLeService mBluetoothLeService;
public String mDeviceAddress="0A:1B:6C:22:11:3D";
private ServiceMonitor serviceMonitor = ServiceMonitor.getInstance();
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
if (!mBluetoothLeService.initialize()) {
Log.e(TAG, "Unable to initialize Bluetooth");
}
mBluetoothLeService.connect(mDeviceAddress);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mBluetoothLeService = null;
}
};
@Override
public void onCreate() {
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
if(mGattUpdateReceiver!=null) {
unregisterReceiver(mGattUpdateReceiver);
}
if(mServiceConnection!=null) {
unbindService(mServiceConnection);
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
//If some device is connecting to phone, let disconnect
if(mConnected==true) {
mBluetoothLeService.disconnect();
}
//If bluetooth is enable
if (BleUtils.getBluetoothAdapter(getApplicationContext()).enable()) {
Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
//Try to reconnect if the connect is not successful
if (mBluetoothLeService != null) {
new CountDownTimer(3000,1000) {
public void onTick(long millisUntilFinished) {}
public void onFinish() {
final boolean result = mBluetoothLeService.connect(mDeviceAddress);
Log.d(TAG, "Connect request result=" + result);
}
}.start();
}
}
return START_STICKY;
}
最佳答案
connectGatt()
方法有不同的参数,它与 API 版本不同。这是 Kotlin 代码。我在任何地方都看不到您的连接方法,但我知道您必须在某处调用 connectGatt()。
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> bluetoothDevice?.connectGatt(applicationContext, true, mGattCallback, BluetoothDevice.TRANSPORT_LE)
else -> bluetoothDevice?.connectGatt(applicationContext, false, mGattCallback)
关于android - 如何解决错误 onClientConnectionState() - status=22 clientIf=7 in BLE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40966104/
场景是: 我有一个 android dongle运行我的应用程序,在另一边我有一个连接到 esp8266 的 HM10 LE 蓝牙。 我正在尝试通过蓝牙将 android 加密狗通过我的应用程序连接到
当我尝试打开通知后连接ble时,经常返回BleGattException status = 133,有时会有时不会。 这是我的代码: mConnectingDevice.establishCo
我有一个 Android 应用程序,它使用 蓝牙低功耗 (BLE) 与我的手机连接。该应用程序在几乎所有手机上运行良好,例如 Galaxy S5 (Android 5.0.1)、Galaxy S6 (
我正在制作 android BLE 应用程序。根据文件,我提出申请。我的问题是,有时应用程序会运行良好,但有时会抛出错误。有 2 个日志,一个用于成功,第二个用于错误。每次蓝牙断开连接时,我都会发送关
我是一名优秀的程序员,十分优秀!