一、什么是蓝牙
1、Bluetooth广泛使用的一种无线通讯协议
2、主要针对短距离通讯(10m)
3、常用于耳机、鼠标、键盘等移动通讯设备
二、与蓝牙相关的API
1、BluetoothAdapter:代表本地蓝牙设备
2、BluetoothDevice:代表远程蓝牙设备
三、蓝牙需要的设置
需要在AndroidManifest中申明蓝牙操作的权限
四、扫描已经配对的蓝牙设备
1、获取BluetoothAdapter对象
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter()
2、判断当前设备中是否有蓝牙设备
if(adapter != null){}
3、判断当前设备中的蓝牙设备是否已经打开
使用adapter.isEnabled()方法
4、得到所有已经配对的蓝牙设备
Set devices = adapter.getBondedDevices();
五、设置蓝牙设备的可见性
1、创建intent对象:
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
2、设置可见性的时间,最大300s,超过300s系统会自动设为300s
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
3、启动Android内置activity,将参数传入
startActivity(discoverableIntent);
六、扫描周围可见的蓝牙
1、调用startDiscovery()来扫描周围可见设备
BluetoothAdapter.getDefaultAdapter().startDiscovery();
2、每次扫描到系统就会发送广播
A、创建一个过滤器,过滤广播ACTION_FOUND
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
B、创建一个广播接收器
BluetoothReceiver bluetoothReceiver = new BluetoothReceiver();
C、 注册接收器
registerReceiver(bluetoothReceiver, intentFilter);
D、在广播中取出数据
String action = intent.getAction();
//从intent取出远程蓝牙设备
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
七、源代码
MainActivity.java
点击(此处)折叠或打开
-
package com.example.bluetooth;
-
-
import java.util.Iterator;
-
import java.util.Set;
-
-
import android.os.Bundle;
-
import android.app.Activity;
-
import android.app.SearchManager.OnCancelListener;
-
import android.bluetooth.BluetoothAdapter;
-
import android.bluetooth.BluetoothDevice;
-
import android.content.BroadcastReceiver;
-
import android.content.Context;
-
import android.content.DialogInterface;
-
import android.content.IntentFilter;
-
import android.content.DialogInterface.OnClickListener;
-
import android.content.Intent;
-
import android.view.Menu;
-
import android.view.View;
-
import android.webkit.WebView.FindListener;
-
import android.widget.Button;
-
import android.widget.EditText;
-
-
public class MainActivity extends Activity
-
{
-
-
private Button findBtn = null;
-
private Button scanBtn = null;
-
private Button setBtn = null;
-
private EditText edit1 = null;
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState)
-
{
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
//找到按钮
-
findBtn = (Button)findViewById(R.id.findBtn);
-
scanBtn = (Button)findViewById(R.id.scanBtn);
-
setBtn = (Button)findViewById(R.id.setBtn);
-
//设置监听器
-
findBtn.setOnClickListener(new btnOnclickListener());
-
scanBtn.setOnClickListener(new btnOnclickListener());
-
setBtn.setOnClickListener(new btnOnclickListener());
-
-
edit1 = (EditText)findViewById(R.id.edit1);
-
//创建一个过滤器,过滤广播ACTION_FOUND
-
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
-
//创建一个广播接收器
-
BluetoothReceiver bluetoothReceiver = new BluetoothReceiver();
-
//注册接收器
-
registerReceiver(bluetoothReceiver, intentFilter);
-
}
-
-
class btnOnclickListener implements android.view.View.OnClickListener
-
{
-
-
@Override
-
public void onClick(View v)
-
{
-
// TODO Auto-generated method stub
-
switch(v.getId())
-
{
-
case R.id.findBtn:
-
//获取BluetoothAdapter对象
-
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
-
//如果得到了设备
-
if(adapter != null)
-
{
-
//更新edittext,打印有设备
-
edit1.append("have!"+'\n');
-
//如果设备没有被打开
-
if(!adapter.isEnabled())
-
{
-
//启动新的activity,打开蓝牙
-
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
-
startActivity(intent);
-
}
-
//获取所有已经配对的蓝牙,得到一个蓝牙设备集
-
SetBluetoothDevice> devices = adapter.getBondedDevices();
-
if(devices.size()>0)
-
{
-
//如果有已经配对的蓝牙,那么用迭代逐个打印出来
-
for(Iterator iterator=devices.iterator(); iterator.hasNext();)
-
{
-
BluetoothDevice bluetoothDevice = (BluetoothDevice)iterator.next();
-
edit1.append(bluetoothDevice.getAddress()+'\n');
-
}
-
}
-
}
-
else
-
{
-
edit1.append("no device!!");
-
}
-
break;
-
case R.id.scanBtn:
-
//调用startDiscovery()来扫描周围可见设备
-
BluetoothAdapter.getDefaultAdapter().startDiscovery();
-
break;
-
case R.id.setBtn:
-
//创建intent通过intent传递参数
-
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
-
//设置可见状态的持续时间,如果超过300s,那么系统会自动设为300s
-
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
-
//启动activity,这是Android自带的
-
startActivity(discoverableIntent);
-
break;
-
default:
-
break;
-
}
-
-
}
-
}
-
-
private class BluetoothReceiver extends BroadcastReceiver
-
{
-
-
@Override
-
public void onReceive(Context context, Intent intent)
-
{
-
// TODO Auto-generated method stub
-
//接受intent
-
String action = intent.getAction();
-
//从intent取出远程蓝牙设备
-
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
-
edit1.append("find new device:"+device.getAddress()+"\n");
-
}
-
-
}
-
@Override
-
public boolean onCreateOptionsMenu(Menu menu)
-
{
-
// Inflate the menu; this adds items to the action bar if it is present.
-
getMenuInflater().inflate(R.menu.main, menu);
-
return true;
-
}
-
- }
点击(此处)折叠或打开
-
RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
-
android:paddingLeft="@dimen/activity_horizontal_margin"
-
android:paddingRight="@dimen/activity_horizontal_margin"
-
android:paddingTop="@dimen/activity_vertical_margin"
-
tools:context=".MainActivity" >
-
-
Button
-
android:id="@+id/findBtn"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:text="扫描已经连接的蓝牙"
-
/>
-
Button
-
android:id="@+id/setBtn"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:layout_below="@+id/findBtn"
-
android:text="设置蓝牙可见性"
-
/>
-
Button
-
android:id="@+id/scanBtn"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:layout_below="@+id/setBtn"
-
android:text="扫描周围可见的蓝牙"
-
/>
-
EditText
-
android:id="@+id/edit1"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:layout_below="@+id/scanBtn"
-
/>
-
- /RelativeLayout>