在Android Studio中配对蓝牙设备 [英] Pair a bluetooth device in Android Studio

查看:1536
本文介绍了在Android Studio中配对蓝牙设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm creating an app that should connect via bluetooth to a specific device.

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

I want my app to connect with this device no matter it is already paired or not.

现在我有这个

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

但是此功能仅连接到已配对的设备. 如果我的设备尚未配对,我想将其配对. 不知道该怎么做.

But this function connects only to paired devices. If my device isn't already paired, I want to pair it. Have no idea how to do this.

有人可以给我任何建议吗?

Can someone get me any advice please?

推荐答案

第一个请求BLUETOOTH_ADMIN权限.

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

Then make your device discoverable:

private void makeDiscoverable() {
        Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
        startActivity(discoverableIntent);
        Log.i("Log", "Discoverable ");
    }

然后创建一个 BroadcastReceiver 来监听系统中的操作:

Then create a BroadcastReceiver to listen to action from system:

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

并通过注册此 BoardcastReceiver 开始搜索设备:

And start searching for devices by registering this BoardcastReceiver:

 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()中选择以下内容:

After devices come from the BroadcastReceiver into a list, select your device from the list and createBond() with this:

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

然后使用上面的代码通过绑定设备进行管理.

Then use your code above to manage with bonded devices.

这篇关于在Android Studio中配对蓝牙设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆