Android初级蓝牙操作

权限

<!-- 蓝牙权限 -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

广播BluetoothrReceiver

public class BluetoothReceiver extends BroadcastReceiver {

    private static final String TAG = "BluetoothReceiver";//此处应不超过23个字符

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(action != null) {
            switch (action) {
                //手机的蓝牙状态
                case BluetoothAdapter.ACTION_STATE_CHANGED:
                    int bluetoothState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
                    switch(bluetoothState) {
                        case BluetoothAdapter.STATE_TURNING_ON:
                            Log.e(TAG, "onReceive---------蓝牙正在打开中");
                            break;
                        case BluetoothAdapter.STATE_ON:
                            Log.e(TAG, "onReceive---------蓝牙已打开");
                            Toast.makeText(context, "蓝牙已打开", Toast.LENGTH_SHORT).show();        
                            break;
                        case BluetoothAdapter.STATE_TURNING_OFF:
                            Log.e(TAG, "onReceive---------蓝牙正在关闭中");
                            break;
                        case BluetoothAdapter.STATE_OFF:
                            Log.e(TAG, "onReceive---------蓝牙已关闭");
                            Toast.makeText(context, "蓝牙已关闭", Toast.LENGTH_SHORT).show();
                            break;
                    }
                    break;
                //蓝牙设备已连接
                case BluetoothDevice.ACTION_ACL_CONNECTED:
                    Log.e(TAG, "onReceive---------蓝牙设备已连接");
                    Toast.makeText(context,"蓝牙设备已连接", Toast.LENGTH_SHORT).show();
                    break;
                //蓝牙设备断开连接
                case BluetoothDevice.ACTION_ACL_DISCONNECTED:
                    Log.e(TAG, "onReceive---------蓝牙设备已断开");
                    Toast.makeText(context,"蓝牙设备已断开",Toast.LENGTH_SHORT).show();         
                    break;
            }
        }
    }
}

Activity

 private BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

 private BluetoothListenerReceiver bluetoothListenerReceiver;//蓝牙状态广播
 
 private void BlueToothON() {
        if(!adapter.isEnabled()) {//如果手机支持蓝牙功能
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            //请求开启蓝牙设备
            startActivity(intent);
        }
        //获得已配对的远程蓝牙设备的集合
        Set<BluetoothDevice> devices = adapter.getBondedDevices();
        if(devices.size() > 0) {
            for(Iterator<BluetoothDevice> it = devices.iterator(); it.hasNext();) {
                BluetoothDevice device = it.next();
                Toast.makeText(MainActivity.this, device.getName(), Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void BlueToothOFF() {
        if(adapter.isEnabled()) {
            //关闭蓝牙设备
            adapter.disable();
        }
    }

    private IntentFilter BluetoothStateFilter() {
        IntentFilter intentFilter = new IntentFilter();
        // 监听手机蓝牙状态
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        //监听蓝牙设备与手机连接的状态
        intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        return intentFilter;
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bluetoothListenerReceiver = new BluetoothListenerReceiver();
        //注册广播
        this.registerReceiver(bluetoothListenerReceiver, BluetoothStateFilter());
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //注销广播
        this.unregisterReceiver(bluetoothListenerReceiver);
    }

执行结果
在这里插入图片描述在这里插入图片描述

BlueToothAdapter

代表手机的蓝牙适配器,可以用来进行比较基础的蓝牙操作,如开关手机蓝牙,扫描外部蓝牙设备,以及获外部蓝牙设备的名称、Mac地址等信息。

string类型

ACTION_DISCOVERY_FINISHED
手机蓝牙适配器已完成外部蓝牙设备的扫描

ACTION_DISCOVERY_STARTED
手机本地蓝牙适配器已开启对外部蓝牙设备的扫描

ACTION_STATE_CHANGED
手机本地蓝牙适配器的状态已发生改变

int类型

STATE_TURNING_ON
手机蓝牙适配器正在打开中

STATE_ON
手机蓝牙适配器已打开

STATE_TURNING_OFF
手机蓝牙适配器正在关闭中

STATE_OFF
手机蓝牙适配器已关闭

BlueToothDevice

代表与手机连接的外部蓝牙设备

string类型

ACTION_ACL_CONNECTED
手机与一个外部蓝牙设备建立低级别(ACL)连接成功

ACTION_ACL_DISCONNECTED
手机与一个外部蓝备设备建立起的低级别(ACL)连接断开

ACTION_ACL_DISCONNECT_REQUESTED
已请求外部蓝牙设备进行低级别(ACL)断开连接,并且将很快断开连接

ACTION_BOND_STATE_CHANGED
外部蓝色设备与手机的绑定状态发生改变

ACTION_CLASS_CHANGED
外部蓝牙设备的类别已更改

ACTION_FOUND
发现外部蓝牙设备

ACTION_NAME_CHANGED
外部蓝牙设备的名称是第一次被检测到,或者自上次检测以来已更改

整型

BOND_BONDED
已绑定(配对)外部蓝牙设备

BOND_BONDING
正在与外部蓝牙设备进行绑定(配对)

BOND_NONE
外部蓝牙设备未绑定(配对)

DEVICE_TYPE_CLASSIC
蓝牙设备类型,经典-BR / EDR设备

DEVICE_TYPE_DUAL
蓝牙设备类型,双模式-BR / EDR / LE

DEVICE_TYPE_UNKNOWN
蓝牙设备类型,未知

Android官方api

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值