0

我正在尝试使用 android 中的 BluetoothGatt API 以编程方式与 Ble iBeacon 配对。我可以与 Ble iBeacon 配对到棒棒糖。但我无法在 Marshmallow 中配对(我的测试设备是 oneplus 3)。我正在使用 altbeacon 库信标扫描。

另外,我在清单文件中授予了 ACCESS_COARSE_LOCATION,ACCESS_FINE_LOCATION 权限并打开了 GPS 定位。

private BluetoothGatt mGatt;
BluetoothAdapter baBluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    baBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    intentFilter = new IntentFilter();
    intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);       
    registerReceiver(myReceiver, intentFilter);
}

我在这里发送信标mac id

public void PairToDevice(String sMacId) {

   BluetoothDevice device = baBluetoothAdapter.getRemoteDevice(sMacId);      

    if (mGatt == null) {
        mGatt = device.connectGatt(this, false, gattCallback);
    }
}
private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        Log.i("onConnectionStateChange", "Status: " + status);
        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");
        }
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        super.onCharacteristicChanged(gatt, characteristic);
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {

    }
}

在这里,我正在检查粘合状态。棉花糖中的粘合状态始终没有。但小于棉花糖它工作正常。

private BroadcastReceiver myReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            int nState = device.getBondState();

            if (nState == BluetoothDevice.BOND_BONDED) {
                Log.e("Bond Information", "device bonded");

            } else if (nState == BluetoothDevice.BOND_BONDING) {
                 Log.e("Bond Information", "device bonding");
            } else if (nState == BluetoothDevice.BOND_NONE) {
                 Log.e("Bond Information", "bond none");
            }
        }
    }
};
4

0 回答 0