gpt4 book ai didi

android - BluetoothGattCallback 为 Beacon 返回 129 -> GATT_INTERNAL_ERROR,但为手机返回 0

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:04:13 27 4
gpt4 key购买 nike

我调用我的蓝牙适配器 mBluetoothAdapter.startDiscovery();,然后返回一个 BluetoothDevices 列表。当我按下其中一个设备时,我会这样做:

   mBluetoothGatt = bluetoothDevice.connectGatt(MainActivity.this, false, mGattCallback);

我的回调在哪里:

   private final BluetoothGattCallback mGattCallback =
new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status,
int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
mConnectionState = STATE_CONNECTED;
Log.i(TAG, "Connected to GATT server.");
Log.i(TAG, "Attempting to start service discovery:" +
mBluetoothGatt.discoverServices());
List<BluetoothGattService> listBGS = mBluetoothGatt.getServices();
Log.i("","list size: " + listBGS.size());

} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
mConnectionState = STATE_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
}
}

@Override
// New services discovered
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.w(TAG, "onServicesDiscovered GATT_SUCCESS: " + status);
List<BluetoothGattService> listBGS = mBluetoothGatt.getServices();
Log.i("","list size: " + listBGS.size());
} 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) {
Log.w(TAG, "onCharacteristicRead GATT_SUCCESS: " + status + " / char: " + characteristic);
}
}
};

现在,当我在手机上尝试时,我在 onConnectionStateChange 上获得 STATE_CONNECTED,然后我调用 discoverServices,我在 ServicesDiscoveres 上返回 GATT_SUCCESS。所以我可以从我的服务中获取 uuid。但是当我在信标上尝试它时,我返回 STATE_CONNECTED,当我调用 discoverServices 时,我返回状态代码:129 -> GATT_INTERNAL_ERROR为什么我的信标会发生这种情况?还有另一种方法,我如何获取设备 uuid?对于 beaconManager,我只需要 UUID 就可以执行此操作:

 try {
beaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", null, null, null));
} catch (RemoteException e) { }

编辑

我知道信标应该要求我提供配对代码,但它没有。为什么?我尝试添加这段代码:

    final IntentFilter pairingRequestFilter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
pairingRequestFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY - 1);
registerReceiver(mPairingRequestRecevier, pairingRequestFilter);

但我仍然没有收到配对和输入密码的请求

这是我的 pairingRequestReceiver:

  private final BroadcastReceiver mPairingRequestRecevier = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(intent.getAction()))
{
final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);

if (type == BluetoothDevice.PAIRING_VARIANT_PIN)
{
device.setPin(intToByteArray(123456));
abortBroadcast();
}
else
{
Log.e("","Unexpected pairing type: " + type);
}
}
}
};

最佳答案

经过几个小时的反复试验,我确实设法找到了一种方法让它像这样工作:1.我以同样的方式开始发现,但是当我选择一个蓝牙设备时,我要求像这样绑定(bind)(配对)它:

 private void pairDevice(BluetoothDevice device) {
try {
Method method = device.getClass().getMethod("createBond", (Class[]) null);
method.invoke(device, (Object[]) null);
} catch (Exception e) {
e.printStackTrace();
}
}

我有一个 ACTION_BOND_STATE_CHANGED 接收器:

 private final BroadcastReceiver mPairReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();

if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
final int prevState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR);

if (state == BluetoothDevice.BOND_BONDED && prevState == BluetoothDevice.BOND_BONDING) {
Log.i("","Paired");
mBluetoothGatt = btDevice.connectGatt(MainActivity.this, false, mGattCallback);
} else if (state == BluetoothDevice.BOND_NONE && prevState == BluetoothDevice.BOND_BONDED){
Log.i("","Unpaired");
}

}
}
};

我是这样注册的:

         IntentFilter intent = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(mPairReceiver, intent);

只有在这之后,我才会尝试连接我的 BluetoothGattCallback。因此,我可以发现服务等

关于android - BluetoothGattCallback 为 Beacon 返回 129 -> GATT_INTERNAL_ERROR,但为手机返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48581916/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com