且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Android的不安全蓝牙连接

更新时间:2022-10-29 17:27:34

  listenUsingInsecureRfcommWithServiceRecord()
 

这是仅适用于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.