Android 蓝牙


  官网https://developer.android.google.cn/guide/topics/connectivity/bluetooth#DiscoveringDevices


蓝牙状态  10 未连接设备  
          12 已连接设备

1.在清单文件,添加蓝牙权限

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

2.打开蓝牙

 //打开蓝牙 (Button的监听)
    public void openBluetooth(View view) {
        //获取蓝牙适配器
        BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
        if (defaultAdapter == null) {
            Toast.makeText(this, "该设备没有蓝牙功能", Toast.LENGTH_SHORT).show();
        } else {
            if (!defaultAdapter.isEnabled()) {//看蓝牙功能是否已开启,没有开启启动蓝牙功能
                Intent enableIntent = new Intent(defaultAdapter.ACTION_REQUEST_ENABLE);//开启蓝牙功能
                startActivityForResult(enableIntent, 300);

            }
        }
       
    }
    
    
     @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 300 && resultCode == RESULT_OK) {
            Toast.makeText(this, "蓝牙开启成功", Toast.LENGTH_SHORT).show();
        } else if (requestCode == 300 && resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "蓝牙开启失败", Toast.LENGTH_SHORT).show();
        }
    }
    
    

3.查找设备

先查找已连接的设备

 //蓝牙适配器已存在的情况下
        if (defaultAdapter != null) {
            //查找已连接设备
            Set<BluetoothDevice> pairedDevices = defaultAdapter.getBondedDevices();
            //假如set集合长度大于0,表示有已连接设备
            if (pairedDevices.size() > 0) {
                for (BluetoothDevice device : pairedDevices) {
                    //添加设备名称到集合
                    Bluetooth mBluetooth = new Bluetooth(device.getName(), device.getAddress(), device.getBondState());
                    list.add(mBluetooth);
                }
            }
        }
        
        
        
4.发现蓝牙设备

 boolean discovery = defaultAdapter.startDiscovery();//发现设备

            //查找蓝牙设备
            IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            //注册广播,处理发现的设备
            registerReceiver(receiver, filter);


 BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                //把找到的设备添加到集合中
                list.add(new Bluetooth(device.getName(), device.getAddress(), device.getBondState()));
            }
            defaultAdapter.cancelDiscovery();//取消发现蓝牙设备
        }
    };

 

 

 

 

 

 

 

 


2.蓝牙API
蓝牙适配器BluetoothAdapter

 

  BluetoothAdapter的作用其实跟其它的**Manger差不多,可以把它当作蓝牙管理器。下面是BluetoothAdapter的常用方法说明。
  
  getDefaultAdapter:静态方法,获取默认的蓝牙适配器对象; 
   enable:打开蓝牙功能; 
   disable:关闭蓝牙功能; 
   isEnable:判断蓝牙功能是否打开; 
   startDiscovery:开始搜索周围的蓝牙设备; 
   cancelDiscovery:取消搜索操作; 
   isDiscovering:判断当前是否正在搜索设备; 
   getBondedDevices:获取已绑定的设备列表; 
   setName:设置本机的蓝牙名称; 
   getName:获取本机的蓝牙名称; 
  getAddress:获取本机的蓝牙地址; 
  getRemoteDevice:根据蓝牙地址获取远程的蓝牙设备; 
  getState:获取本地蓝牙适配器的状态; 
  listenUsingRfcommWithServiceRecord:根据名称和UUID创建并返回BluetoothServiceSocket; 
  listenUsingRfcommOn:根据渠道编号创建并返回BluetoothServiceSocket。

 

蓝牙设备BluetoothDevice

 

  BluetoothDevice用于指代某个蓝牙设备,通常表示对方设备。BluetoothAdapter管理的是本机蓝牙设备。下面是BluetoothDevice的常用方法说明。
  
  getName:获得该设备的名称; 
   getAddress:获得该设备的地址; 
   getBondState:获得该设备的绑定状态; 
   createBond:创建匹配对象; 
   createRfcommSocketToServiceRecord:根据UUID创建并返回一个BluetoothSocket。

 

蓝牙服务器套接字BluetoothServiceSocket

 

  BluetoothServiceSocket是服务端的Socket,用来接收客户端的Socket连接请求。下面是常用的方法说明。
  
  accept:监听外部的蓝牙连接请求; 
   close:关闭服务端的蓝牙监听。

 

蓝牙客户端套接字BluetoothSocket

 

  BluetoothSocket是客户端的Socket,用于与对方设备进行数据通信。下面是常用的方法说明。
  
  connect:建立蓝牙的socket连接; 
    close:关闭蓝牙的socket连接; 
   getInputStream:获取socket连接的输入流对象; 
   getOutputStream:获取socket连接的输出流对象; 
   getRemoteDevice:获取远程设备信息。

---------------------

本文来自 _彼岸雨敲窗_ 的优快云 博客 ,全文地址请点击:https://blog.youkuaiyun.com/fukaimei/article/details/78837578?utm_source=copy 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值