一. 什么是蓝牙(bluetooth)?
1.1 buletooth是目前使用最广泛的无线通信协议
1.2 主要针对短距离设备通讯(10m)
1.3 常用于连接耳机,鼠标和移动通讯设备等.
二. 与蓝牙相关的api
2.1 bluetoothadapter:
代表了本地的蓝牙适配器
2.2 bluetoothdevice
代表了一个远程的bluetooth设备
三. 扫描已经配对的蓝牙设备(1)
注:必须部署在真实手机上,模拟器无法实现
首先需要在androidmanifest.xml 声明蓝牙权限
<user-permission android:name="android.permission.bluetooth" />
配对蓝牙需要手动操作:
1. 打开设置--> 无线网络 --> 蓝牙 勾选开启
2. 打开蓝牙设置 扫描周围已经开启的蓝牙设备(可以与自己的笔记本电脑进行配对),点击进行配对
电脑上会弹出提示窗口: 添加设备
显示计算与设备之间的配对码,要求确认是否配对
手机上也会显示类似的提示.
四. 扫描已经配对的蓝牙设备(2)
4.1 获得bluetoothadapter对象
4.2 判断当前移动设备中是否拥有蓝牙
4.3 判断当前移动设备中蓝牙是否已经打开
4.4 得到所有已经配对的蓝牙设备对象
蓝牙配对实现的核心代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
mainactivity:
import java.util.iterator;
import java.util.set;
import android.app.activity;
import android.bluetooth.bluetoothadapter;
import android.bluetooth.bluetoothdevice;
import android.content.intent;
import android.os.bundle;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
public class mainactivity extends activity {
private button button = null ;
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
setcontentview(r.layout.main);
button = (button)findviewbyid(r.id.buttonid);
button.setonclicklistener( new onclicklistener(){
@override
public void onclick(view v) {
//获得bluetoothadapter对象,该api是android 2.0开始支持的
bluetoothadapter adapter = bluetoothadapter.getdefaultadapter();
//adapter不等于null,说明本机有蓝牙设备
if (adapter != null ){
system.out.println( "本机有蓝牙设备!" );
//如果蓝牙设备未开启
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 = (bluetoothdevice)it.next();
//打印出远程蓝牙设备的物理地址
system.out.println(device.getaddress());
}
} else {
system.out.println( "还没有已配对的远程蓝牙设备!" );
}
} else {
system.out.println( "本机没有蓝牙设备!" );
}
}
});
}
}
|
修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备
1. 修改本机蓝牙设备的可见性
2. 扫描周围可用的蓝牙设备
eg:
一. 清单文件adroidmanifest.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?xml version= "1.0" encoding= "utf-8" ?>
<manifest xmlns:android= "http://schemas.android.com/apk/res/android"
package = "com.se7en"
android:versioncode= "1"
android:versionname= "1.0" >
<uses-sdk android:minsdkversion= "8" />
<application android:icon= "@drawable/icon" android:label= "@string/app_name" >
<activity android:name= ".mainactivity"
android:label= "@string/app_name" >
<intent-filter>
<action android:name= "android.intent.action.main" />
<category android:name= "android.intent.category.launcher" />
</intent-filter>
</activity>
</application>
<uses-permission android:name= "android.permission.bluetooth" />
<!-若需要管理蓝牙设备,如修改可见性,则需以下的权限->
<uses-permission android:name= "android.permission.bluetooth_admin" />
</manifest>
|
二. 布局文件: main.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?xml version= "1.0" encoding= "utf-8" ?>
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:orientation= "vertical"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
>
<textview
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:text= "@string/hello"
/>
<button
android:id= "@+id/discoverbutton"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:text= "设置可见性" />
<button
android:id= "@+id/scanbutton"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:text= "开始扫描" />
</linearlayout>
|
三. mainactivity:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
import android.app.activity;
import android.bluetooth.bluetoothadapter;
import android.bluetooth.bluetoothdevice;
import android.content.broadcastreceiver;
import android.content.context;
import android.content.intent;
import android.content.intentfilter;
import android.os.bundle;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
public class mainactivity extends activity {
private button discoverbutton = null ;
private button scanbutton = null ;
private bluetoothadapter adapter = null ;
private bluetoothreceiver bluetoothreceiver = null ;
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
setcontentview(r.layout.main);
adapter = bluetoothadapter.getdefaultadapter();
discoverbutton = (button)findviewbyid(r.id.discoverbutton);
scanbutton = (button)findviewbyid(r.id.scanbutton);
//修改蓝牙设备的可见性
discoverbutton.setonclicklistener( new onclicklistener(){
@override
public void onclick(view view) {
intent discoverintent = new intent(bluetoothadapter.action_request_discoverable);
//设置蓝牙可见性,500表示可见时间(单位:秒),当值大于300时默认为300
discoverintent.putextra(bluetoothadapter.extra_discoverable_duration, 500 );
startactivity(discoverintent);
}
});
scanbutton.setonclicklistener( new onclicklistener(){
@override
public void onclick(view v) {
//开始扫描周围蓝牙设备,该方法是异步调用并以广播的机制返回,所以需要创建一个broadcastreceiver来获取信息
adapter.startdiscovery();
}
});
//设定广播接收的filter
intentfilter intentfilter = new intentfilter(bluetoothdevice.action_found);
//创建蓝牙广播信息的receiver
bluetoothreceiver = new bluetoothreceiver ();
//注册广播接收器
registerreceiver(bluetoothreceiver,intentfilter);
}
private class bluetoothreceiver extends broadcastreceiver{
@override
public void onreceive(context context, intent intent) {
//获得扫描到的远程蓝牙设备
bluetoothdevice device = intent.getparcelableextra(bluetoothdevice.extra_device);
system.out.println(device.getaddress());
}
}
}
|