连接到String上具有蓝牙地址的设备

Mar*_*all 16 sockets string android bluetooth

我正在做一个Android应用程序,我将另一个设备的MAC作为字符串(长度为17个字符),并且需要使用那个以连接到该设备(启动蓝牙连接的线程).我整个下午一直在玩它,无法弄清楚如何做到这一点.问题是它不允许我将BluetoothDevice设置为等于字符串.有没有办法可以/必须这样做?

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

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

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

任何帮助将受到高度赞赏,

Sto*_*ica 28

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

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;
Run Code Online (Sandbox Code Playgroud)

这是这个简单的开源Android应用程序源代码的摘录:https: //github.com/janosgyerik/bluetoothviewer

该应用程序是一个简单的工具,用于调试蓝牙连接和原始协议数据.(目前仅在ascii中,我计划添加调试十六进制的功能.)


siv*_*ash 6

将字符串值转换为蓝牙设备。

BluetoothManager bluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothDevice mBluetoothDevice = bluetoothManager.getAdapter() .getRemoteDevice("deviceAddress");
Run Code Online (Sandbox Code Playgroud)