Android-Bluetooth
2015-12-19 16:44
423 查看
Android provides rich APIs to let your app connect and interact with other devices over Bluetooth, NFC, Wi-Fi P2P, USB, and SIP, in addition to standard network connections.
The application framework provides access to the Bluetooth functionality through the Android Bluetooth
APIs. These APIs let applications wirelessly connect to other Bluetooth devices, enabling point-to-point and multipoint wireless features.
Using the Bluetooth APIs, an Android application can perform the following:
Scan for other Bluetooth devices
Query the local Bluetooth adapter for paired Bluetooth devices
Establish RFCOMM channels
Connect to other devices through service discovery
Transfer data to and from other devices
Manage multiple connections
This document describes how to use the Android Bluetooth APIs to accomplish the four major tasks necessary to communicate using Bluetooth: setting up Bluetooth, finding devices that
are either paired or available in the local area, connecting devices, and transferring data between devices.
here's how you can register to handle the broadcast when devices are discovered:
Caution: Performing device discovery is a heavy procedure for the Bluetooth adapter and will consume
a lot of its resources. Once you have found a device to connect, be certain that you always stop discovery with
attempting a connection. Also, if you already hold a connection with a device, then performing discovery can significantly reduce the bandwidth available for the connection, so you should not perform discovery while connected.
、、A Universally Unique Identifier (UUID) is a standardized 128-bit format for a string ID used to
uniquely identify information. The point of a UUID is that it's big enough that you can select any random and it won't clash.
》 Here's a simplified thread for the server component that accepts incoming connections:
Starting in Android 3.0, the Bluetooth API includes support for working with Bluetooth profiles. A Bluetooth
profileis a wireless interface specification for Bluetooth-based communication between devices.
》The Android Bluetooth API provides implementations for the following Bluetooth profiles:
Headset. The Headset profile provides support for Bluetooth headsets to be used with mobile phones. Android provides the
which is a proxy for controlling the Bluetooth Headset Service via interprocess communication (IPC).
This includes both Bluetooth Headset and Hands-Free (v1.5) profiles. The
includes support for AT commands. For more discussion of this topic, see Vendor-specific AT commands
A2DP. The Advanced Audio Distribution Profile (A2DP) profile defines how high quality audio can be streamed from one device to another over a Bluetooth connection. Android provides the
which is a proxy for controlling the Bluetooth A2DP Service via IPC.
Health Device. Android 4.0 (API level 14) introduces support for the Bluetooth Health Device Profile (HDP). This lets you create applications that use Bluetooth to communicate with health devices that support
Bluetooth, such as heart-rate monitors, blood meters, thermometers, scales, and so on. For a list of supported devices and their corresponding device data specialization codes, refer to Bluetooth Assigned Numbers atwww.bluetooth.org.
Note that these values are also referenced in the ISO/IEEE 11073-20601 [7] specification as MDC_DEV_SPEC_PROFILE_* in the Nomenclature Codes Annex. For more discussion of HDP, see Health
Device Profile.
》For example, this code snippet shows how to connect to a
object so that you can control the Headset profile:
》Android 4.0 (API level 14) introduces support for the Bluetooth Health Device Profile (HDP). This lets you create applications that use Bluetooth to communicate with health devices
that support Bluetooth, such as heart-rate monitors, blood meters, thermometers, and scales.
The application framework provides access to the Bluetooth functionality through the Android Bluetooth
APIs. These APIs let applications wirelessly connect to other Bluetooth devices, enabling point-to-point and multipoint wireless features.
Using the Bluetooth APIs, an Android application can perform the following:
Scan for other Bluetooth devices
Query the local Bluetooth adapter for paired Bluetooth devices
Establish RFCOMM channels
Connect to other devices through service discovery
Transfer data to and from other devices
Manage multiple connections
This document describes how to use the Android Bluetooth APIs to accomplish the four major tasks necessary to communicate using Bluetooth: setting up Bluetooth, finding devices that
are either paired or available in the local area, connecting devices, and transferring data between devices.
<uses-permission android:name="android.permission.BLUETOOTH" />
here's how you can register to handle the broadcast when devices are discovered:
// Create a BroadcastReceiver for ACTION_FOUND private final BroadcastReceiver mReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); // When discovery finds a device if (BluetoothDevice.ACTION_FOUND.equals(action)) { // Get the BluetoothDevice object from the Intent BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // Add the name and address to an array adapter to show in a ListView mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); } } }; // Register the BroadcastReceiver IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
Caution: Performing device discovery is a heavy procedure for the Bluetooth adapter and will consume
a lot of its resources. Once you have found a device to connect, be certain that you always stop discovery with
cancelDiscovery()before
attempting a connection. Also, if you already hold a connection with a device, then performing discovery can significantly reduce the bandwidth available for the connection, so you should not perform discovery while connected.
、、A Universally Unique Identifier (UUID) is a standardized 128-bit format for a string ID used to
uniquely identify information. The point of a UUID is that it's big enough that you can select any random and it won't clash.
》 Here's a simplified thread for the server component that accepts incoming connections:
private class AcceptThread extends Thread { private final BluetoothServerSocket mmServerSocket; public AcceptThread() { // Use a temporary object that is later assigned to mmServerSocket, // because mmServerSocket is final BluetoothServerSocket tmp = null; try { // MY_UUID is the app's UUID string, also used by the client code tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID); } catch (IOException e) { } mmServerSocket = tmp; } public void run() { BluetoothSocket socket = null; // Keep listening until exception occurs or a socket is returned while (true) { try { socket = mmServerSocket.accept(); } catch (IOException e) { break; } // If a connection was accepted if (socket != null) { // Do work to manage the connection (in a separate thread) manageConnectedSocket(socket); mmServerSocket.close(); break; } } } /** Will cancel the listening socket, and cause the thread to finish */ public void cancel() { try { mmServerSocket.close(); } catch (IOException e) { } } }
Starting in Android 3.0, the Bluetooth API includes support for working with Bluetooth profiles. A Bluetooth
profileis a wireless interface specification for Bluetooth-based communication between devices.
》The Android Bluetooth API provides implementations for the following Bluetooth profiles:
Headset. The Headset profile provides support for Bluetooth headsets to be used with mobile phones. Android provides the
BluetoothHeadsetclass,
which is a proxy for controlling the Bluetooth Headset Service via interprocess communication (IPC).
This includes both Bluetooth Headset and Hands-Free (v1.5) profiles. The
BluetoothHeadsetclass
includes support for AT commands. For more discussion of this topic, see Vendor-specific AT commands
A2DP. The Advanced Audio Distribution Profile (A2DP) profile defines how high quality audio can be streamed from one device to another over a Bluetooth connection. Android provides the
BluetoothA2dpclass,
which is a proxy for controlling the Bluetooth A2DP Service via IPC.
Health Device. Android 4.0 (API level 14) introduces support for the Bluetooth Health Device Profile (HDP). This lets you create applications that use Bluetooth to communicate with health devices that support
Bluetooth, such as heart-rate monitors, blood meters, thermometers, scales, and so on. For a list of supported devices and their corresponding device data specialization codes, refer to Bluetooth Assigned Numbers atwww.bluetooth.org.
Note that these values are also referenced in the ISO/IEEE 11073-20601 [7] specification as MDC_DEV_SPEC_PROFILE_* in the Nomenclature Codes Annex. For more discussion of HDP, see Health
Device Profile.
》For example, this code snippet shows how to connect to a
BluetoothHeadsetproxy
object so that you can control the Headset profile:
BluetoothHeadset mBluetoothHeadset; // Get the default adapter BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); // Establish connection to the proxy. mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET); private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() { public void onServiceConnected(int profile, BluetoothProfile proxy) { if (profile == BluetoothProfile.HEADSET) { mBluetoothHeadset = (BluetoothHeadset) proxy; } } public void onServiceDisconnected(int profile) { if (profile == BluetoothProfile.HEADSET) { mBluetoothHeadset = null; } } }; // ... call functions on mBluetoothHeadset // Close proxy connection after use. mBluetoothAdapter.closeProfileProxy(mBluetoothHeadset);
》Android 4.0 (API level 14) introduces support for the Bluetooth Health Device Profile (HDP). This lets you create applications that use Bluetooth to communicate with health devices
that support Bluetooth, such as heart-rate monitors, blood meters, thermometers, and scales.
相关文章推荐
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
- Hibernate之Query.uniqueResult()结果为数值的注意事项
- 代码手写UI,xib和StoryBoard间的博弈,以及Interface Builder的一些小技巧
- ios控件-UILabel
- AutoLayout处理UITableView动态高度
- Arduino - IIC SCANNER
- 【iOS学习笔记 15-12-19】自定义cell侧滑按钮(UIButton)
- UIWindow和UIView的简单使用
- 黑马程序员_Java基础-GUI
- iOS学习之 iOS9新特性UIStackView
- dequeueReusableCellWithIdentifier: forIndexPath: 获取cell
- 分享一个使用NSIS制作安装包的UI插件
- 13、提升Android UI流畅度的建议
- 用数据库连接池c3p0来连接数据库,用queryrunner来操作
- [深入浅出Windows 10]QuickCharts图表控件库解析
- UILabel的一些整理
- UIViewAnimationOptions
- self.view 添加子视图 和 简单的 UIView 的组合动画
- DSOJ Sliding Window
- UIPickerView 创建中国地区显示 省份 市