在Android的不安全蓝牙连接蓝牙、不安全、Android

2023-09-05 08:24:46 作者:觅你だ风川间

我已经由一个教授开发在Android有点蓝牙演示应用程序的挑战。我也不知道,深化发展为Android,直到2个星期前,当他给我的挑战。我也很新,在一般的Java编程,所以我从很远的开始。但无论如何...

所以,我做了大部分的教程,和我在Android中读到蓝牙,看着蓝牙聊天样品code,和我现在试图做我的小应用程序。所以对于我的演示,我将尝试建立我的真正的手机和我的蓝牙鼠标之间的连接。我想将我的手机屏幕上的一个形状,回应我的鼠标移动。

我遇到很多问题,但到目前为止,我主要的一个是用我的不安全鼠标打开一个套接字。当我尝试使用方法 listenUsingRfcommWithServiceRecord ,它要求一个UUID作为参数。但是,我的鼠标最有可能没有一个UUID回应,所以我想这个方法是不是好的。

当我阅读文档,有关此方法,它说,与像老鼠的设备打开不安全的服务器套接字,我必须使用 listenUsingInsecureRfcommWithServiceRecord 方法。但这种方法不可用时,我称呼它,它就会被红色下划线和Eclipse说,它是不确定的类型BluetoothAdapter。

 私人BluetoothServerSocket connectDevice(BluetoothAdapter适配器,BluetoothDevice类设备){
    BluetoothServerSocket插座= NULL;
    尝试{
        插座= adapter.listenUsingInsecureRfcommWithServiceRecord(device.getName(),UUID.randomUUID());
    }
    赶上(IOException异常E){
        Toast.makeText(这一点,连接失败\ñ。+ e.getMessage(),Toast.LENGTH_SHORT);
    }

    返回插座;
}
 

请不要火焰我,如果我做的都错了,这是我在这里的第一个问题,我开始用Java编程。

解决方案

  listenUsingInsecureRfcommWithServiceRecord()
 
win7系统下电脑与Android手机蓝牙连接方法

这是仅适用于API级别10和更高版本,即安卓v2.3.3起。

这可能是问题,如果你正在构建一个版本previous了这一点。

请参阅在文档

编辑:在光的事实,这是不可能的延长BluetoothAdapter, listenUsingInsecureRfcommWithServiceRecord()简单地做到这一点...

 返回createNewRfcommSocketAndRecord(名称,UUID,假的,假的);
 

在源createNewRfcommSocketAndRecord()(这是BluetoothAdapter的私有方法),可以在这里找到... createNewRfcommSocketAndRecord

不知道是否会帮助,但你也许能够复制它的功能。

I have been challenged by a professor to develop a little Bluetooth Demo app on Android. I knew nothing about developping for Android until 2 weeks ago when he gave me that challenge. I'm also quite new at Java programming in general, so I'm starting from far. But anyway...

So I did most of the tutorial, and I read about Bluetooth in Android, looked at the Bluetooth Chat sample code, and I'm now trying to do my little app. So for my demo, I will try to establish a connection between my real phone and my Bluetooth mouse. I want to move a shape on the screen of my phone in response to my mouse movement.

I encounter many problem, but so far my main one is to open a socket with my unsecure mouse. When I try using the method listenUsingRfcommWithServiceRecord, it ask a UUID as a parameter. But my mouse most likely doesn't have a UUID to respond, so I guess this method is not the good one.

When I read the documentation about this method, it says that to open an unsecure server socket with a device like a mouse, I must use the listenUsingInsecureRfcommWithServiceRecord method. But this method is not available when I call it, it gets underlined in red and Eclipse says that it is undefined for the type BluetoothAdapter.

private BluetoothServerSocket connectDevice(BluetoothAdapter adapter, BluetoothDevice device){
    BluetoothServerSocket socket = null;
    try{
        socket = adapter.listenUsingInsecureRfcommWithServiceRecord(device.getName(), UUID.randomUUID());
    }
    catch(IOException e){
        Toast.makeText(this, "Connection failed.\n" + e.getMessage(), Toast.LENGTH_SHORT);
    }

    return socket;
}

Please don't flame me if I'm doing it all wrong, it's my first question here and I'm starting with Java programming.

解决方案

listenUsingInsecureRfcommWithServiceRecord()

This is only available on API Level 10 and later, i.e., Android v2.3.3 onwards.

That may be the problem if you're building for a version previous to that.

See to the right-hand side of the grey bar in the docs

EDIT: In light of the fact it isn't possible to extend BluetoothAdapter, listenUsingInsecureRfcommWithServiceRecord() simply does this...

return createNewRfcommSocketAndRecord(name, uuid, false, false);

The source for createNewRfcommSocketAndRecord() (which is a private method of BluetoothAdapter), can be found here...createNewRfcommSocketAndRecord

Not sure if will help but you might be able to reproduce its functionality.