1》在AndroidManifest.xml文件中添加相关权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapp.bluetoothdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<!-- 1.添加蓝牙使用权限 -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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>
</manifest>
2》获取信息,具体详见代码注示2,3,4,5
package com.myapp.bluetoothdemo;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import android.support.v7.app.ActionBarActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.os.RemoteCallbackList;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
// 声明控件
private Button scanBlueDev_btn;
// 本地蓝牙设备
private BluetoothAdapter localBlueTooth;
// 与本地蓝牙设备配对的远程蓝牙设备
private Set<BluetoothDevice> remoteBlueToothes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
scanBlueDev_btn = (Button) findViewById(R.id.scanBlueDev_btn);
scanBlueDev_btn.setOnClickListener(blueDevClickListener);
}
OnClickListener blueDevClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.scanBlueDev_btn:
scanBlueDevices();
break;
default:
break;
}
}
};
private void scanBlueDevices() {
// 2.得到本地蓝牙设备
localBlueTooth = BluetoothAdapter.getDefaultAdapter();
// 3. 判断本地是否有蓝牙设备
if (localBlueTooth != null) {// 本地有蓝牙设备
// 4.判断本地蓝牙设备是否已经打开
if (!localBlueTooth.isEnabled()) {// 本地蓝牙未打开,提示打开蓝牙
Intent blueToothIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(blueToothIntent);
}
// 5.获取已配对的远程蓝牙设备
// 注意:即使本地蓝牙未打开,也可以得到已配对的远程蓝牙设备,因为这些信息已经存储在本地了。
remoteBlueToothes = localBlueTooth.getBondedDevices();
if (remoteBlueToothes.size() > 0) {
for (Iterator iterator = remoteBlueToothes.iterator(); iterator
.hasNext();) {
BluetoothDevice bluetoothDevice = (BluetoothDevice) iterator
.next();
Log.i("CXC", bluetoothDevice.getAddress());
}
}
} else {// 本地没有蓝牙设备
Log.i("CXC", "本地没有蓝牙设备");
}
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}