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

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

问题描述

我在做一个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?

(决定不把任何我尝试在这里为code,看他们是如何充满了错误)

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

它有运行完全相同的应用程序的另一个平板电脑进行通信。我通过这个页面早些时候,大部分我的应用程序是基于这一点。使用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?

任何帮助将是非常美联社preciated,

Any help would be highly appreciated,

推荐答案

如果我理解正确的话,你有一个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;

这是从这个简单的开源Android应用程序的源代码code的摘录:
<一href=\"https://github.com/janosgyerik/bluetoothviewer\">https://github.com/janosgyerik/bluetoothviewer

This is an excerpt from the source code of this simple open-source Android App: https://github.com/janosgyerik/bluetoothviewer

该应用程序可用于调试蓝牙连接和原始协议数据的简单工具。 (目前只在ASCII码,我打算增加新的功能调试十六进制为好。)

The app is a simple tool for debugging Bluetooth connections and raw protocol data. (For now only in ascii, I plan to add features to debug hexadecimal as well.)

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

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