android蓝牙相关

1.android蓝牙的几个基本概念

  • 权限
<uses-permissionandroid:name="[Android](http://lib.csdn.net/base/android).permission.BLUETOOTH" />
允许程序连接到已配对的蓝牙设备。
<uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN" />
允许程序发现和配对蓝牙设备。
  • 操作蓝牙主要用到的类 BluetoothAdapter类,使用时导包
import android.bluetooth.BluetoothAdapter;

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();  
  • 开启蓝牙
 if(!mBluetoothAdapter.isEnabled()){  
//弹出对话框提示用户是后打开  
Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  
startActivityForResult(enabler, REQUEST_ENABLE);  
      //不做提示,直接打开,不建议用下面的方法,有的手机会有问题。  
      // mBluetoothAdapter.enable();  
}  
  • 获取本地蓝牙信息

//获取本机蓝牙名称  
String name = mBluetoothAdapter.getName();  
//获取本机蓝牙地址  
String address = mBluetoothAdapter.getAddress();  
Log.d(TAG,"bluetooth name ="+name+" address ="+address);  
//获取已配对蓝牙设备  
Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();  
Log.d(TAG, "bonded device size ="+devices.size());  
for(BluetoothDevice bonddevice:devices){  
    Log.d(TAG, "bonded device name ="+bonddevice.getName()+" address"+bonddevice.getAddress());  
}  
  • 搜索设备:
    mBluetoothAdapter.startDiscovery();
    开始搜索设备,该过程是异步的,通过下面注册广播接受者,可以监听是否搜到设备。
IntentFilter filter = new IntentFilter();  
//发现设备  
filter.addAction(BluetoothDevice.ACTION_FOUND);  
//设备连接状态改变  
filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);  
//蓝牙设备状态改变  
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);  
registerReceiver(mBluetoothReceiver, filter);  
  • 监听扫描结果
private BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver(){  
        @Override  
        public void onReceive(Context context, Intent intent) {  
            String action = intent.getAction();  
            Log.d(TAG,"mBluetoothReceiver action ="+action);  
            if(BluetoothDevice.ACTION_FOUND.equals(action)){//每扫描到一个设备,系统都会发送此广播。  
                //获取蓝牙设备  
                BluetoothDevice scanDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);  
                if(scanDevice == null || scanDevice.getName() == null) return;  
                Log.d(TAG, "name="+scanDevice.getName()+"address="+scanDevice.getAddress());  
                //蓝牙设备名称  
                String name = scanDevice.getName();  
                if(name != null && name.equals(VnApplication.BLUETOOTH_NAME)){  
                    mBluetoothAdapter.cancelDiscovery();  
                    //取消扫描  
                    mProgressDialog.setTitle(getResources().getString(R.string.progress_connecting));                   //连接到设备。  
                    mBlthChatUtil.connect(scanDevice);  
                }  
            }else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){  
  
            }  
        }  
          
    };  
  • 连接:
    此方法中主要是获取socket客户端,并连接。
BluetoothSocket  mmSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
mmSocket.connect();
  • 连接成功后,获取输入输出流,进行通信
    当连接成功后,connected(mmSocket, mmDevice); 获取输入输出流,从而进行通信。
    private InputStream mmInStream = socket.getInputStream();
    private OutputStream mmOutStream =socket.getOutputStream();

mmOutStream.write(buffer);发送信息。
mmInStream.read(buffer); 收到消息。

Paste_Image.png
Paste_Image.png
Paste_Image.png
Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.简介 通过蓝牙API,可以实现以下内容: 扫描其他蓝牙设备 查询配对蓝牙设备的本地蓝牙适配器 创建RFCOMM...
    justCode_阅读 10,520评论 0 3
  • 蓝牙 注:本文翻译自https://developer.android.com/guide/topics/conn...
    RxCode阅读 12,860评论 11 99
  • Android 蓝牙相关的广播 监听蓝牙相关的广播并获得相关的信息,蓝牙相关的广播主要集中在BluetoothAd...
    CoderMiner阅读 14,293评论 8 13
  • 前言 此外本文只涉及经典蓝牙(Classic Bluetooth)的点对点通信开发,并不涉及低功耗蓝牙(BLE)的...
    杨卢阳阅读 9,737评论 5 15
  • 行归路,打王者。 一人行,三人行。 醉者,醒者。 谁在醉,谁在醒。 又有谁是真的行路者。 行归路,游戏败。 时而左...
    疯子_bda9阅读 3,222评论 3 1

友情链接更多精彩内容