此篇博文主要讲的是蓝牙:它包括蓝牙的可见性设置、以及扫描周围的蓝牙设备
manifests里面所加的权限:
<!--扫描已配对的蓝牙设备权限-->
<uses-permission android:name="android.permission.BLUETOOTH" />
<!--修改蓝牙设备的可见性的权限-->
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>
主程序,代码里面已经有详细的注解,这里就不多加叙述了。
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button mButton;
private Button mButtonDiscover;
private Button mButtonScan;
private BluetoothAdapter bluetoothAdapter;
private BluetoothReceiver bluetoothReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//得到BluetoothAdapter的对象
bluetoothAdapter= BluetoothAdapter.getDefaultAdapter();
//创建一个IntentFilter对象,将其action指定为BluetoothDevice.ACTION_FOUND
IntentFilter intentFilter=new IntentFilter(BluetoothDevice.ACTION_FOUND);
bluetoothReceiver=new BluetoothReceiver();
//注册广播接收器
registerReceiver(bluetoothReceiver,intentFilter);
mButton= (Button) findViewById(R.id.button_bluetooth);
mButton.setOnClickListener(this);
mButtonDiscover= (Button) findViewById(R.id.discover);
mButtonDiscover.setOnClickListener(this);
mButtonScan= (Button) findViewById(R.id.scan_bluetooth);
mButtonScan.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button_bluetooth:
getBlueToothAddress();
break;
case R.id.discover:
//设置监听器用来修改蓝牙设备的可见性,设置一个intent对象,将其action值设置为BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE
Intent discoverintent=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
//将一个键值对存放在intent对象当中,主要用来指定可见状态的持续时间
discoverintent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,120);
startActivity(discoverintent);
break;
case R.id.scan_bluetooth:
bluetoothAdapter.startDiscovery();
break;
}
}
public class BluetoothReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)){
//可以从收到的intent对象中,将代表远程蓝牙的适配器取出
BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d("bluetooth",device.getAddress());
}
}
}
/*
用来判断是否有蓝牙设备,如果有蓝牙设备,扫描并得到已经配对的蓝牙设备的Mac地址
*/
private void getBlueToothAddress() {
//判断BluetoothAdapter对象是否为空,如果为空,则表示本机没有蓝牙设备
if (bluetoothAdapter!=null){
//调用isEnabled()方法,判断当前蓝牙设备是否可用
if (!bluetoothAdapter.isEnabled()){
//如果蓝牙设备不可用,创建一个Intent对象,该对象用于启动一个Activity,提示用户开启蓝牙设备
Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(intent);
}
//得到所有已配对的蓝牙适配器的对象
Set<BluetoothDevice> devices=bluetoothAdapter.getBondedDevices();
if (devices.size()>0){
for (Iterator iterator=devices.iterator();iterator.hasNext();){
BluetoothDevice bluetoothDevice= (BluetoothDevice) iterator.next();
//得到远程蓝牙设备的地址
Log.d("bluetooth", bluetoothDevice.getAddress());
}
}
}else {
Log.d("bluetooth","没有蓝牙设备");
}
}
}
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/button_bluetooth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="扫描周围蓝牙设备"/>
<Button
android:id="@+id/discover"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="设置可见性"/>
<Button
android:id="@+id/scan_bluetooth"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="开始扫描"/>
</LinearLayout>