Java 在Android Studio中配对蓝牙设备

Java 在Android Studio中配对蓝牙设备,java,android,bluetooth,Java,Android,Bluetooth,我正在创建一个应通过蓝牙连接到特定设备的应用程序 我希望我的应用程序与此设备连接,无论它是否已配对 现在我有这个 private void findDevice() { Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices(); if (pairedDevices.size() > 0) { for (BluetoothDevice device : pair

我正在创建一个应通过蓝牙连接到特定设备的应用程序

我希望我的应用程序与此设备连接,无论它是否已配对

现在我有这个

private void findDevice() {
    Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
    if (pairedDevices.size() > 0) {
        for (BluetoothDevice device : pairedDevices) {
            if (device.getName().equals(DEVICE_NAME)) {
                bluetoothDevice = device;
                deviceFound = true;
                break;
            }
        }
    }
}
private void findDevice(){
Set pairedDevices=bluetoothAdapter.getBondedDevices();
如果(pairedDevices.size()>0){
用于(蓝牙设备:pairedDevices){
if(device.getName().equals(device_NAME)){
蓝牙设备=设备;
deviceFound=true;
打破
}
}
}
}
但此功能仅连接到配对设备。 如果我的设备尚未配对,我想配对。 我不知道怎么做


有人能给我一些建议吗?

第一次请求
BLUETOOTH\u ADMIN
许可

然后使您的设备可被发现:

private void makeDiscoverable() {
        Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
        startActivity(discoverableIntent);
        Log.i("Log", "Discoverable ");
    }
然后创建一个广播接收器,以收听来自系统的操作:

private BroadcastReceiver myReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Message msg = Message.obtain();
            String action = intent.getAction();
            if(BluetoothDevice.ACTION_FOUND.equals(action)){
               //Found, add to a device list
            }           
        }
    };
并通过注册此Board Castreceiver开始搜索设备:

 private void startSearching() {
        Log.i("Log", "in the start searching method");
        IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        BluetoothDemo.this.registerReceiver(myReceiver, intentFilter);
        bluetoothAdapter.startDiscovery();
    }
设备从BroadcastReceiver进入列表后,从列表中选择您的设备,然后使用以下命令
createBond()

 public boolean createBond(BluetoothDevice btDevice)  
    throws Exception  
    { 
        Class class1 = Class.forName("android.bluetooth.BluetoothDevice");
        Method createBondMethod = class1.getMethod("createBond");  
        Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);  
        return returnValue.booleanValue();  
    } 

然后使用上面的代码管理绑定设备。

您是否请求了BLUETOOTH_ADMIN权限?是的,我请求了您是否尝试:老实说,我在理解该主题中的代码时遇到问题。使用Set pairedDevice=bluetoothAdapter.getBondedDevices()对Android studio.Im来说是全新的;要获得所有已绑定的设备,只需比较它们的名称,就可以找到我愿意连接的设备。但是在为未绑定设备创建相同的列表时遇到了问题。老兄,非常感谢,这似乎是合法的。但是在接收方面有问题。Android Studio说我无法解析符号“BroadcastReceiver”。我在谷歌上搜索答案,但找不到答案:(@Kirchhoff1415什么问题?好的,知道了,我错过了导入android.content.BroadcastReceiver;part。你成功了吗?好的,我对“创建的债券”有问题function.im getting无法使用“Method”和“invoke”解析符号,我已经从中完成了所有导入,但没有帮助:(