android学习笔记之蓝牙

什么是蓝牙

1、Bluetooth是目前使用的最广泛的无线通讯协议

2、主要针对短距离设备通讯(10M)

3、常用于连接耳机、鼠标和移动通讯设备等

 

与蓝牙相关的API

1、BluetoothAdapter:该类的对象代表了本地的蓝牙适配器

2、BluetoothDevice:代表了一个远程的Bluetooth设备

 

 

扫描已配对的蓝牙设备(一)

1、在AndroidManifest.xml声明蓝牙权限:

<uses-permission android:name= “android.permission.BLUETOOTH”/>

 

扫描已配对的蓝牙设备(二)

1、获得BluetoothAdapter对象

BluetoothAdapteradapter = BluetoothAdapter.getDefaultAdapter();

2、判断当前设备中是否拥有蓝牙设备

if(adapter !=null)  //如果adapter为空,则表明本机没有蓝牙设备

3、判断当前设备中的蓝牙设备是否已经打开

if(!adapter.isEnabled())  //判断当前蓝牙设备是否可用

{

       //创建一个Intent对象,该对象用于启动一个Activity,提示用户开启蓝牙设备

       Intent intent = newIntent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

       startActivity(intent);   

}

4、得到所有已经配对的蓝牙设备对象

Set<BluetoothDevice>devices = adapter.getBondedDevices();

if(devices.size()> 0)

{

       for(Iterator iterator = devices.iterator();iterator.hasNext();)

       {

              BluetoothDevice bluetoothDevice =(BluetoothDevice)iterator.next();

       }

}

 

如果需要对蓝牙的可见性进行修改,还需要加权限

<uses-permission android:name= “android.permission.BLUETOOTH_ADMIN”/>

 

//设置蓝牙可见

Intent discoverableIntent = newIntent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);

discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATOIN,500); //最高不超过300秒,这里设置500秒,也会当300秒

startActivity(discoverableIntent);

 

 

BluetoothAdapterbluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

bluetoothAdapter.startDiscovery();  //每次扫描到一个设备,都会发送一个广播

 

//创建一个广播接收器来进行接收

private classBluetoothReceiver extends 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);

                     System.out.println(device.getAddress());

}

       }

}

.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值