安卓蓝牙笔记:SPP和BLE安卓代码对比

本文探讨了经典蓝牙(SPP协议)与低功耗蓝牙(BLE协议)的区别,指出SPP用于经典蓝牙,BLE适用于低功耗场景。文章通过非完整的代码示例展示了两者在权限声明、设备发现、连接建立和管理方面的差异,并提到了官方文档链接供深入学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前自己做好毕业设计,帮同学看代码,我使用的是BLE蓝牙的支持BLE数据透传,他那边说是使用SPP协议的蓝牙支持SPP数据透传。网上查到的比较复杂凌乱,说BLE,SPP都是协议。我寻思BLE不是指低功耗蓝牙么,怎么感觉不大对。

经过查阅了几十个参考资料,得出了SPP协议是经典蓝牙使用的,BLE协议是低功耗蓝牙(低功耗速度慢)使用的。也有说BLE蓝牙不支持SPP,不能传输语音,做编码可以传输语音,经典蓝牙可以直接传输语音等。蓝牙还有一种双模蓝牙,可以根据使用情况动态切换协议。毕竟不是主要研究蓝牙的,如果不对请指正。

经典蓝牙和BLE蓝牙的实现方式代码是不一样的,但也有通用的部分。

经典蓝牙的是用BluetoothSocket来实现。收发都是新建线程来实现的

BLE用的是GATT server.收发是利用广播实现的

非完整代码,仅仅是理解。

以下提供了BLE蓝牙代码和经典蓝牙的代码(代码来自官网示例)对比,有耐心可以看一下,贴下两个官方的

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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值