之前自己做好毕业设计,帮同学看代码,我使用的是BLE蓝牙的支持BLE数据透传,他那边说是使用SPP协议的蓝牙支持SPP数据透传。网上查到的比较复杂凌乱,说BLE,SPP都是协议。我寻思BLE不是指低功耗蓝牙么,怎么感觉不大对。
经过查阅了几十个参考资料,得出了SPP协议是经典蓝牙使用的,BLE协议是低功耗蓝牙(低功耗速度慢)使用的。也有说BLE蓝牙不支持SPP,不能传输语音,做编码可以传输语音,经典蓝牙可以直接传输语音等。蓝牙还有一种双模蓝牙,可以根据使用情况动态切换协议。毕竟不是主要研究蓝牙的,如果不对请指正。
经典蓝牙和BLE蓝牙的实现方式代码是不一样的,但也有通用的部分。
经典蓝牙的是用BluetoothSocket来实现。收发都是新建线程来实现的
BLE用的是GATT server.收发是利用广播实现的
非完整代码,仅仅是理解。
以下提供了BLE蓝牙代码和经典蓝牙的代码(代码来自官网示例)对比,有耐心可以看一下,贴下两个官方的
蓝牙设备通信需要拥有一个唯一的UUID
权限
要声明使用蓝牙权限(经典蓝牙和BLE蓝牙声明相同)
<manifest ... >
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<!-- If your app targets Android 9 or lower, you can declare
ACCESS_COARSE_LOCATION instead. -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
...
</manifest>
如果要app只允许BLE设备,补充如下声明
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
建立
经典蓝牙:
//获取BluetoothAdapter
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
// Device doesn't support Bluetooth
}
//使能
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
BLE蓝牙:
//获取BluetoothAdapter
private BluetoothAdapter bluetoothAdapter;
...
// Initializes Bluetooth adapter.
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
//使能蓝牙
// Ensures Bluetooth is available on the device and it is enabled. If not,
// displays a dialog requesting user permission to enable Bluetooth.
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
查询发现设备
经典蓝牙:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Register for broadcasts when a device is discovered.
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, filter);
}
// Create a BroadcastReceiver for ACTION_FOUND.
private final BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
}
}
};
@Override
protected void onDestroy() {
super.onDestroy();
...
// Don't forget to unregister the ACTION_FOUND receiver.
unregisterReceiver(receiver);
}
BLE蓝牙:
/**
* Activity for scanning and displaying available BLE devices.
*/
public class DeviceScanActivity extends ListActivity {
private BluetoothAdapter bluetoothAdapter;
private boolean mScanning;
private Handler handler;
// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 10000;
...
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
handler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
bluetoothAdapter.stopLeScan(leScanCallback);
}
}, SCAN_PERIOD);
mScanning = true;
bluetoothAdapter.startLeSca