简体   繁体   English

Android中不安全的蓝牙连接

[英]Unsecured Bluetooth connection in Android

I have been challenged by a professor to develop a little Bluetooth Demo app on Android. 我被一位教授挑战在Android上开发一个小型蓝牙演示应用程序。 I knew nothing about developping for Android until 2 weeks ago when he gave me that challenge. 直到两周前,当他给我这个挑战时,我对Android的开发一无所知。 I'm also quite new at Java programming in general, so I'm starting from far. 我对Java编程也很陌生,所以我从远处开始。 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. 所以我做了大部分的教程,我读到了Android中的蓝牙,查看了蓝牙聊天示例代码,我现在正在尝试做我的小应用程序。 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. 当我尝试使用listenUsingRfcommWithServiceRecord方法listenUsingRfcommWithServiceRecord ,它会将UUID作为参数。 But my mouse most likely doesn't have a UUID to respond, so I guess this method is not the good one. 但是我的鼠标很可能没有UUID来响应,所以我猜这种方法并不好。

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. 当我阅读有关此方法的文档时,它说要使用像鼠标这样的设备打开不安全的服务器套接字,我必须使用listenUsingInsecureRfcommWithServiceRecord方法。 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. 但是当我调用它时,这个方法是不可用的,它以红色加下划线,而Eclipse表示它对于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. 如果我做错了,请不要激怒我,这是我的第一个问题,我开始使用Java编程。

listenUsingInsecureRfcommWithServiceRecord()

This is only available on API Level 10 and later, ie, Android v2.3.3 onwards. 这仅适用于API级别10及更高版本,即Android v2.3.3及更高版本。

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... 编辑:鉴于无法扩展BluetoothAdapter的事实, listenUsingInsecureRfcommWithServiceRecord()只是这样做...

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

The source for createNewRfcommSocketAndRecord() (which is a private method of BluetoothAdapter), can be found here... createNewRfcommSocketAndRecord createNewRfcommSocketAndRecord()(这是BluetoothAdapter的私有方法)的源代码可以在这里找到... createNewRfcommSocketAndRecord

Not sure if will help but you might be able to reproduce its functionality. 不确定是否会有所帮助,但您可以重现其功能。

If you are trying to talk to a commercial mouse - then using the SPP socket APIs in android will not help, The mice uses the HID Bluetooth profile , and it requires the phone to have the HID profile host role available. 如果你试图与商业鼠标交谈 - 那么在Android中使用SPP套接字API将无济于事,鼠标使用HID蓝牙配置文件,它要求手机具有HID配置文件主机角色。 Standard android release don't support HID currently - so you will have to add it yourself and build android integrating HID from BlueZ and connecting it to your application. 标准的Android版本目前不支持HID - 所以你必须自己添加它并构建android集成来自BlueZ的HID并将其连接到你的应用程序。

For Bluetooth profile support to be implemented on Android, there is a project called “Sybase-iAnywhere-Blue-SDK-for-Android”, which replaces Android's version, and provides all interfaces into the underlying Bluetooth profiles and protocols. 要在Android上实现蓝牙配置文件支持,有一个名为“Sybase-iAnywhere-Blue-SDK-for-Android”的项目,它取代了Android的版本,并提供了所有接口到底层蓝牙配置文件和协议。 Using this, printing over bluetooth using your Android phone will be possible using the BPP profile provided by this SDK. 使用此功能,可以使用此SDK提供的BPP配置文件,使用您的Android手机通过蓝牙打印。

See links below for more details: link 1: http://www.sybase.com/detail?id=1064424 有关更多详细信息,请参阅以下链接:链接1: http//www.sybase.com/detail?id = 1064424

Link 2: http://www.sybase.com/products/allproductsa-z/mobiledevicesdks/bluetoothsdks 链接2: http//www.sybase.com/products/allproductsa-z/mobiledevicesdks/bluetoothsdks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM