gpt4 book ai didi

android - 如何使用 RxAndroidBLE 在 BLE 中创建绑定(bind)或配对

转载 作者:行者123 更新时间:2023-11-29 18:42:27 25 4
gpt4 key购买 nike

我想为连接设备实现密码或与BLE设备建立连接。我有 6 字节 key ,那么我们将如何使用 RxAndroid 库创建绑定(bind)或配对并将 key 发送到 BLE 设备。

任何人都可以给我引用或文档链接或演示链接以实现密码或在建立连接时创建绑定(bind)或配对

提前致谢!

最佳答案

首先您需要找到附近的设备。

public void registerDeviceForAnyNEwDeviceFound() {
// Register for broadcasts when a device is discovered.
mBluetoothAdapter.startDiscovery();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mSearchReceiver, filter);
Log.e("STATUS", "" +
mBluetoothAdapter.isDiscovering());
isSearchReceiverRegistered = true;
}

// Create a BroadcastReceiver for ACTION_FOUND.
private final BroadcastReceiver mSearchReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.e("SEARCH STARTED", "TRUE");
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
try {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
Device mDevice = new Device();
mDevice.setDeviceName(deviceName);
mDevice.setDeviceAddress(deviceHardwareAddress);
mDevice.setmBluetoothDevice(device);

Log.e("Discovered DEVICE", deviceName);
Log.e("BOND STATE", "" + device.getBondState());

nearbyDeviceList.add(mDevice);
mNearbyDeviceRecAdapter.notifyDataSetChanged();

} catch (Exception e) {
Log.e("EXCEPTION", e.getLocalizedMessage());
}

}
}
};

然后您需要注册另一个 BroadCastReceiver,这个接收器将监听与您要配对的设备的绑定(bind)变化。

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(mPairingReceiver, filter);

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

if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
BluetoothDevice mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//3 cases:
//case1: bonded already
if (mDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
Log.d(TAG, "BroadcastReceiver: BOND_BONDED.");
Log.e(TAG, "BroadcastReceiver: BOND_BONDED." + mDevice.getName());
mBluetoothAdapter.cancelDiscovery();

}
//case2: creating a bone
if (mDevice.getBondState() == BluetoothDevice.BOND_BONDING) {
Log.d(TAG, "BroadcastReceiver: BOND_BONDING.");
Log.e(TAG, "BroadcastReceiver: BOND_BONDING." + mDevice.getName());
}
//case3: breaking a bond
if (mDevice.getBondState() == BluetoothDevice.BOND_NONE) {
Log.d(TAG, "BroadcastReceiver: BOND_NONE.");
Log.e(TAG, "BroadcastReceiver: BOND_NONE." + mDevice.getName());
}
}
}
};

最后当你想与设备通话配对时:

mNearbyDeviceRecAdapter.getItemAtPosition(position).getmBluetoothDevice().createBond();

调用此语句后,将启动与设备的配对。

希望!这有帮助。

关于android - 如何使用 RxAndroidBLE 在 BLE 中创建绑定(bind)或配对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52827736/

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