Ble Beacon Pairing在Marshmallow中不起作用

时间:2016-12-20 10:09:48

标签: android bluetooth-lowenergy ibeacon altbeacon beacon

对于我的应用程序,我尝试使用BluetoothGatt API以编程方式与Ble Beacon配对。我可以将Ble信号与棒棒糖配对。但我不能在Marshmallow配对(我的测试设备是oneplus 3)。

此外,我在清单文件中提供了ACCESS_COARSE_LOCATION,ACCESS_FINE_LOCATION权限并打开了GPS位置。

BluetoothGatt mGatt; 
baBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
public void connectToDevice(String sMacId) {        

    BluetoothDevice device = baBluetoothAdapter.getRemoteDevice(sMacId);       

    if (mGatt == null) {
        mGatt = device.connectGatt(this, false, gattCallback);
    }
}

这是我的BluetoothGatt回调方法。在调用discoverServices()后,它直接转到BluetoothProfile.STATE_DISCONNECTED case。

private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

        switch (newState) {
            case BluetoothProfile.STATE_CONNECTED:
                Log.e("gattCallback", "STATE_CONNECTED");
                gatt.discoverServices();
                break;
            case BluetoothProfile.STATE_DISCONNECTED:
                Log.e("gattCallback", "STATE_DISCONNECTED");
                break;
            default:
                Log.e("gattCallback", "STATE_OTHER");
        }
    }
}

我也试过原生蓝牙。但它显示错误。我的信标配对密码是123456.This密码工作不到marshmallow.Please帮助我! Please see this image link

1 个答案:

答案 0 :(得分:0)

如果有人仍在为此寻找解决方案,请参阅stackoverflow.com上的this answer,在gatt.connect()完成后添加mDevice.connectGatt(mContext, false, mBluetoothGattCallback);