Android 中不安全的蓝牙连接
一位教授向我提出挑战,要求我在 Android 上开发一个小型蓝牙演示应用程序。直到两周前他向我提出这个挑战之前,我对 Android 开发一无所知。一般来说,我对 Java 编程也很陌生,所以我的起点还很远。但无论如何......
所以我完成了大部分教程,我阅读了 Android 中的蓝牙,查看了蓝牙聊天示例代码,现在我正在尝试做我的小应用程序。因此,在我的演示中,我将尝试在我的真实手机和蓝牙鼠标之间建立连接。我想根据鼠标的移动来移动手机屏幕上的形状。
我遇到了很多问题,但到目前为止我的主要问题是用不安全的鼠标打开套接字。当我尝试使用方法 listenUsingRfcommWithServiceRecord
时,它会询问 UUID 作为参数。但我的鼠标很可能没有 UUID 来响应,所以我想这种方法不是一个好的方法。
当我阅读有关此方法的文档时,它说要使用鼠标等设备打开不安全的服务器套接字,我必须使用 listenUsingInsecureRfcommWithServiceRecord
方法。但当我调用该方法时,该方法不可用,它会以红色下划线显示,并且 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;
}
如果我做错了,请不要责怪我,这是我的第一个问题,我将从 Java 编程开始。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这仅适用于 API Level 10 及更高版本,即 Android v2.3.3 及以上版本。
如果您正在构建之前的版本,这可能就是问题所在。
请参阅 docs
编辑:鉴于不可能扩展BluetoothAdapter,
listenUsingInsecureRfcommWithServiceRecord()
只是这样做...createNewRfcommSocketAndRecord() 的源代码(这是BluetoothAdapter 的私有方法),可以在这里找到...createNewRfcommSocketAndRecord
不确定是否有帮助,但您也许能够重现其功能。
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...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.
如果您尝试与商业鼠标通信 - 那么在 android 中使用 SPP 套接字 API 将无济于事,鼠标使用 HID 蓝牙配置文件,并且需要手机具有可用的 HID 配置文件主机角色。
标准 Android 版本当前不支持 HID - 因此您必须自己添加它并构建集成 BlueZ 的 HID 的 Android 并将其连接到您的应用程序。
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.
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 上实现的蓝牙配置文件支持,有一个名为“Sybase-iAnywhere-Blue-SDK-for-Android”的项目,该项目替换了 Android 的版本,并提供了底层蓝牙配置文件和协议的所有接口。使用此 SDK 提供的 BPP 配置文件,您可以使用 Android 手机通过蓝牙进行打印。
请参阅以下链接了解更多详细信息:
链接1:
http://www.sybase.com/detail?id=1064424
链接 2:
http://www.sybase.com/products/allproductsa-z/mobiledevicesdks/bluetoothsdks
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. Using this, printing over bluetooth using your Android phone will be possible using the BPP profile provided by this SDK.
See links below for more details:
link 1:
http://www.sybase.com/detail?id=1064424
Link 2:
http://www.sybase.com/products/allproductsa-z/mobiledevicesdks/bluetoothsdks