代码之家  ›  专栏  ›  技术社区  ›  rosu alin

BluetoothGattCallback为信标返回129->GATT\u INTERNAL\u错误,但为电话返回0

  •  7
  • rosu alin  · 技术社区  · 6 年前

    我使用蓝牙适配器 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\u,然后我调用DiscoveryServices,我在Services Discoverys上获得了GATT\u的成功。所以我可以从我的服务中获取UUID。 但当我在信标上尝试时,我会返回STATE\u CONNECTED,当我调用discoverServices时,我会返回一个状态代码:129->GATT\u内部错误 为什么我的信标会这样?是否有其他方法,我如何获取设备uuid? 我只需要UUID就可以在beaconManager之后执行此操作:

     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 回复  |  直到 6 年前
        1
  •  3
  •   rosu alin    6 年前

    经过数小时的反复试验,我终于找到了一种方法,使其工作如下: 1、我同样启动发现,但当我选择一个蓝牙设备时,我会要求与它绑定(配对),如下所示:

     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\u BOND\u STATE\u已更改的收件人:

     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连接。因此,我可以发现服务,等等