代码之家  ›  专栏  ›  技术社区  ›  Nitin Karande

如何在BLE中使用RxAndroidBLE创建绑定或配对

  •  0
  • Nitin Karande  · 技术社区  · 6 年前

    我想实施 用于连接设备或与BLE设备建立连接。我有 6字节密钥 ,那么我们将如何创建绑定或配对,并使用RxAndroid库将密钥发送到BLE设备。

    在建立连接时实现密钥或创建绑定或配对 .

    1 回复  |  直到 6 年前
        1
  •  3
  •   Udit Kapahi    6 年前

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

    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());
                }
    
            }
        }
    };
    

    然后你需要注册另一个广播接收器,这个接收器将监听与你将要配对的设备的绑定变化。

    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();
    

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

    希望!这有帮助。