首先在程序里我们要开启蓝牙权限
在应用程序manifest文件中添加如下代码,声明蓝牙权限。
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
ListView的适配器用于展示蓝牙名字和地址
public class LeDeviceListAdapter extends BaseAdapter {
private ArrayList<BluetoothDevice> mLeDevices;
private Activity mActivity;
private LayoutInflater mInflater;
public LeDeviceListAdapter(Activity mActivity) {
this.mActivity = mActivity;
mInflater = mActivity.getLayoutInflater();
mLeDevices = new ArrayList<BluetoothDevice>();
}
@Override
public int getCount() {
return mLeDevices.size();
}
@Override
public Object getItem(int i) {
return mLeDevices.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
public void addDevice(BluetoothDevice device){
if(!mLeDevices.contains(device)){
mLeDevices.add(device);
}
}
public void clear(){
mLeDevices.clear();
}
private BluetoothDevice getDevice(int position){
return mLeDevices.get(position);
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if(view == null){
view = mInflater.inflate(R.layout.device_list, null);
viewHolder = new ViewHolder();
viewHolder.deviceName = (TextView)view.findViewById(R.id.device_name);
viewHolder.deviceAddress = (TextView)view.findViewById(R.id.device_address);
view.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)view.getTag();
}
BluetoothDevice device = mLeDevices.get(i);
viewHolder.deviceName.setText(device.getName());
viewHolder.deviceAddress.setText(device.getAddress());
return view;
}
class ViewHolder{
TextView deviceName;
TextView deviceAddress;
}
}
蓝牙初始化配置
public class BluetoothLess {
private static final String TAG = "BluetoothLess";
private Context mContext;
public BluetoothAdapter getmBluetoothAdapter() {
return mBluetoothAdapter;
}
public void setmBluetoothAdapter(BluetoothAdapter mBluetoothAdapter) {
this.mBluetoothAdapter = mBluetoothAdapter;
}
public BluetoothLess(Context mContext) {
this.mContext = mContext;
}
private BluetoothAdapter mBluetoothAdapter;
private BluetoothManager mBluetoothManager;
public boolean initialied(){
if (mBluetoothManager == null) {
mBluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
if (mBluetoothManager == null) {
Log.i(TAG, "Unable to initialize BluetoothManager");
return false;
}
}
mBluetoothAdapter = mBluetoothManager.getAdapter();
if (mBluetoothAdapter == null) {
Log.i(TAG, "Unable to obtain a BluetoothAdapter.");
return false;
}
// 直接启动蓝牙
boolean isEnableBTA = mBluetoothAdapter.enable();
if (isEnableBTA) {
Log.i(TAG, "mBluetoothAdapter.enable");
} else {
Log.i(TAG, "mBluetoothAdapter.disable");
}
return isEnableBTA;
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/start_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始扫描"/>
<Button
android:id="@+id/stop_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止扫描"/>
<ListView
android:id="@+id/device_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button start_btn;
private Button stop_btn;
private ListView device_list;
private LeDeviceListAdapter adapter;
private BluetoothLess mBluetoothLess;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initBinds();
initDatas();
mBluetoothLess.initialied();
}
private void initDatas() {
mBluetoothLess = new BluetoothLess(this);
adapter = new LeDeviceListAdapter(this);
device_list.setAdapter(adapter);
}
private void initBinds() {
start_btn = (Button)findViewById(R.id.start_btn);
stop_btn = (Button)findViewById(R.id.stop_btn);
device_list = (ListView)findViewById(R.id.device_list);
start_btn.setOnClickListener(this);
stop_btn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.start_btn:
mBluetoothLess.getmBluetoothAdapter().startLeScan(mLeScanCallback);
break;
case R.id.stop_btn:
mBluetoothLess.getmBluetoothAdapter().startLeScan(mLeScanCallback);
break;
}
}
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice bluetoothDevice, int i, byte[] bytes) {
runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.addDevice(bluetoothDevice);
adapter.notifyDataSetChanged();
}
});
}
};
}