使用字符串上的蓝牙地址连接到设备 [英] connect to device with Bluetooth address on String

查看:17
本文介绍了使用字符串上的蓝牙地址连接到设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个 Android 应用程序,其中我将另一台设备的 MAC 作为字符串(17 个字符长),并且需要使用该字符串才能连接到该设备(启动蓝牙连接的线程).我整个下午都在玩它,但不知道该怎么做.问题是它不允许我将 BluetoothDevice 设置为一个字符串.有没有办法做到/必须做到这一点?

I am doing an Android App and where I have the MAC of another device as a string (17 characters long) and need to use that one in order to connect to that device (thread that initiates a Bluetooth connection). I've been playing around with it all afternoon and can't figure out how to do it. The problem is that it doesn't allow me to set the BluetoothDevice equal to a string. Is there a way that this can/has to be done?

(决定不把我的任何尝试作为代码放在这里,看看它们是如何充满错​​误的)

(decided not to put any of my attempts here as code, seeing how they were full of errors)

它必须与另一台运行相同应用程序的平板电脑进行通信.我之前浏览了 this 页面,我的大部分应用程序都基于该页面.我的主要问题是在使用 ConnectThread 示例时,

It has to communicate with another tablet that is running the exact same application. I looked through this page earlier and most of my app is based on that. My main problem is when using the ConnectThread example,

我有一个包含 MAC 地址的字符串,我如何连接到该 MAC?

I have a string with the MAC address, how do I connect to that MAC?

任何帮助将不胜感激,

推荐答案

如果我理解正确,你有一个 MAC 地址作为字符串,并且你想连接到设备,对吗?这应该有效:

If I understand correctly, you have a MAC address as a string, and you want to connect to the device, right? This should work:

BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
BluetoothSocket tmp = null;
BluetoothSocket mmSocket = null;

// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
try {
    tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
    Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
    tmp = (BluetoothSocket) m.invoke(device, 1);
} catch (IOException e) {
    Log.e(TAG, "create() failed", e);
}
mmSocket = tmp;

这篇关于使用字符串上的蓝牙地址连接到设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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