Android蓝牙学习——搜索、配对、传文件(附源码)

导语

蓝牙作为一种成熟、低功耗无线通信技术的先锋,在可穿戴设备领域中扮演着越来越重要的作用。目前流行的蓝牙成功案例在运动手环、行车记录仪、终端解锁、智能家居等领域。接下来,一起动手敲代码吧~

源码下载:http://download.csdn.net/download/csdn_aiyang/9973522

添加权限

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

BluetoothAdapter

获取实例:BluetoothAdapter mBluetoothAdapter = BluetoothAdapter .getDefaultAdapter () ;
 if (mBluetoothAdapter == null) {
            Toast.makeText(this, "设备不支持蓝牙", Toast.LENGTH_SHORT).show();
            return;
    }

常用方法:

  • isEnabled() 判断系统蓝牙是否打开
  • disable() 无弹窗提示关闭系统蓝牙 
  • enable()  无弹窗提示打开系统蓝牙(此操作有点不友好!) 
  • startDiscovery() 开始搜索设备 —–适合经典蓝牙和低功耗蓝牙两种
  • cancelDiscovery() 取消搜索设备
  • startLeScan() 开始搜索设备 —–适合扫描低功耗蓝牙,但是在api21以上被标记废弃使用
  • stopLeScan() 停止搜索设备
  • startScan()  开始搜索设备 —–api21以上扫描低功耗蓝牙,通过bluetoothAdapter.getBluetoothLeScanner()方法获取
  • stopScan() 停止搜索设备
  • stopScan() 停止搜索设备
注册广播监听

    private IntentFilter makeFilters() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        intentFilter.addAction("android.bluetooth.BluetoothAdapter.STATE_OFF");
        intentFilter.addAction("android.bluetooth.BluetoothAdapter.STATE_ON");
        intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
        intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        return intentFilter;
    }
     BTBroadcastReceiver receiver = new BTBroadcastReceiver();
        registerReceiver(receiver, makeFilters());
        receiver.setBRInteractionListener(this);
然后在自定义广播类中进行拦截Action操作,再使用自定义接口与Activity进行交互数据信息显示。(可在源码中查看参考)


发送文本

/**
 * 蓝牙UUID
 */
public static UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

BluetoothSocket bluetoothSocket =BluetoothDevice.createRfcommSocketToServiceRecord(Comment.SPP_UUID); 
OutputStream outputStream =bluetoothSocket.getOutputStream();
            outputStream.write(txtString.getBytes("utf-8"));
            outputStream.flush();


相关地址下载:

GitHub:https://github.com/aiyangtianci/BluetoothAPP

码云:蓝牙demogiteehttps://gitee.com/AiYangDian/bluetoothdemo


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

艾阳Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值