Ski*_*ᴉʞS 1 android android-bluetooth
我正在尝试使用我的Android APP连接到特定设备,直到现在我能够做的是获得配对项目:
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set < BluetoothDevice > pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device: pairedDevices) {
mDeviceName.add(device.getName());
mDeviceMAC.add(device.getAddress());
}
}
bluetoothClass.setDeviceName(mDeviceName);
bluetoothClass.setDeviceMac(mDeviceMAC);
Run Code Online (Sandbox Code Playgroud)
在那里我得到了MAC和Device name我所有配对设备的.我想做的是当我选择一个连接到Bluetooth设备的时候.
在Samsung S4当我打开Bluetooth它弹出式菜单一Dialogwhitch包含了所有我的配对设备,当我点击任何它连接(我我能够......),所以基本上我想这样做,因为现在我已经获得配对的设备(我不知道它是否是获得它的最佳方式,但确实如此)然后当用户点击任何连接到设备的人时.
它就像这个问题,但不幸的是没有答案.
在这种格式下给你一个例子是不可能的,所以我为你提供了一个很好的样本和有用的链接,以帮助你理解样本.
我建议您按照我提供的步骤进行操作,然后,当您遇到特定问题时,可以使用您遇到困难的代码段将其带到此处.
我建议您使用下载此示例代码:http:
//developer.android.com/samples/BluetoothChat/index.html
如果你还没有,那么研究这个很好:http:
//developer.android.com/reference/android/bluetooth/BluetoothDevice.html
这是一个很好的教程,他们有很多教程:http:
//www.tutorialspoint.com/android/android_bluetooth.htm
您需要在清单中具有以下权限:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
Run Code Online (Sandbox Code Playgroud)
这是一个建议使用的意图,以检查是否启用了BT:
if (!mBluetoothAdapter.isEnabled()) {
android.content.Intent enableIntent = new android.content.Intent(
android.bluetooth.BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
}
Run Code Online (Sandbox Code Playgroud)
并使您的设备可被其他设备发现:
if (mBluetoothAdapter.getScanMode() !=
android.bluetooth.BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
android.content.Intent discoverableIntent =
new android.content.Intent(
android.bluetooth.BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(
android.bluetooth.BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
300); // You are able to set how long it is discoverable.
startActivity(discoverableIntent);
}
Run Code Online (Sandbox Code Playgroud)
正如我在答案中提到的那样:
您可以避免使用意图搜索配对设备.连接到未配对的设备时,会弹出一个通知,要求配对设备.配对后,此消息不应再显示这些设备,连接应该是自动的(根据您编写程序的方式).
我使用意图启用蓝牙,并使我的设备可被发现,然后我设置我的代码进行连接,然后按一个按钮进行连接.在您的情况下,您还需要确保您的配件也可以被发现.在我的情况下,我使用一个唯一的UUID,两个设备必须识别这个连接.这只能用于编程两个设备,无论是Android还是一个android和另一个设备类型.
您需要了解如何使用套接字,这就是设备的通信方式.
我建议研究这两个链接:
http://developer.android.com/reference/android/bluetooth/BluetoothSocket.html
http://developer.android.com/reference/android/bluetooth/BluetoothServerSocket.html
套接字用于设置设备之间的连接.将有一个服务器套接字和设备套接字(由许多因素决定,程序员,实际设备).服务器套接字侦听传入连接,当接受连接时,设备连接,每个连接都有一个简单的套接字.
我不确定你对线程有多了解.需要使用线程管理连接:
http://developer.android.com/guide/components/processes-and-threads.html
http://android-developers.blogspot.com.au/2009/05/painless-threading.html
设备之间的连接在与用户界面线程分开的线程上进行管理.这是为了防止手机在设置,寻找和建立BT连接时锁定.
例如:
AcceptThread - 侦听连接并接受连接的线程(通过serversocket).该线程可以运行一段时间等待设备连接.
ConnectThread - 连接到服务器的设备用于连接到serversocket的线程.
ConnectedThread - 这是管理两个套接字之间连接的线程.
如果这有助于您,请告诉我.
| 归档时间: |
|
| 查看次数: |
10793 次 |
| 最近记录: |