Как создать Bond или Pairing в BLE с помощью RxAndroidBLE - PullRequest
0 голосов
/ 16 октября 2018

Я хочу реализовать пароль для подключения устройства или установить соединение с устройством BLE.У меня есть 6-байтовый пароль , так как мы создадим связь или соединение и отправим пароль на устройство BLE с помощью библиотеки RxAndroid.

Может ли кто-нибудь дать мне ссылку или ссылку на документ или демонстрационную ссылку для реализации Passkey или создать связь или соединение при установлении соединения .

Заранее спасибо !!

1 Ответ

0 голосов
/ 16 октября 2018

Сначала вам нужно найти близлежащие устройства.

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, этот ресивер будет прослушивать изменения связи с устройством, с которым вы собираетесь установить соединение.

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

Как только вы вызовете этот оператор, это инициирует сопряжение с устройством.

Надеюсь!это помогает.

...