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

android – BluetoothGattCallback为Beacon返回129 – > GATT_INTERNAL_ERROR,但为手机返回0,第1张

概述我打电话给我的蓝牙适配器mBluetoothAdapter.startDiscovery();然后返回一个BluetoothDevices列表.当我按下其中一个设备时,我这样做: mBluetoothGatt = bluetoothDevice.connectGatt(MainActivity.this, false, mGattCallback); 我的回调是: private final Bl 我打电话给我的蓝牙适配器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,"onCharacteristicRead GATT_SUCCESS: " + status + " / char: " + characteristic);                }            }        };

现在,当我在手机上试用它时,我在onConnectionStateChange上获得了STATE_CONNECTED,然后我调用了discoverServices,然后我返回了一个GATT_SUCCESS onServicesdiscoveres.所以我可以从我的服务中取出uuIDs.
但是当我在信标上尝试它时,我回到STATE_CONNECTED,当我调用discoverServices时,我得到一个状态代码:129 – > GATT_INTERNAL_ERROR
为什么我的信标会发生这种情况?还有另外一种方法,我怎么能拿设备uuID?
对于beaconManager,我只需要UUID能够执行此 *** 作:

try {        beaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueID",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.我开始发现相同,但当我选择蓝牙设备时,我要求与它绑定(配对):
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,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全部内容,希望文章能够帮你解决android – BluetoothGattCallback为Beacon返回129 – > GATT_INTERNAL_ERROR,但为手机返回0所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1135154.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存