1,添加权限
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
2,蓝牙控制类
package com.win16.bluetoothclass4;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import java.util.ArrayList;
import java.util.List;
/** * Created by Rex on 2015/5/27. */
public class BlueToothController {
private BluetoothAdapter mAapter;
public BlueToothController() {
mAapter = BluetoothAdapter.getDefaultAdapter();
}
public BluetoothAdapter getAdapter() {
return mAapter;
}
/** * 打开蓝牙 * @param activity * @param requestCode */
public void turnOnBlueTooth(Activity activity, int requestCode) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
activity.startActivityForResult(intent, requestCode);
// mAdapter.enable();
}
/** * 打开蓝牙可见性 * @param context */
public void enableVisibly(Context context) {
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
context.startActivity(discoverableIntent);
}
/** * 查找设备 */
public void findDevice() {
assert (mAapter != null);
mAapter.startDiscovery();
}
/** * 获取绑定设备 * @return */
public List<BluetoothDevice> getBondedDeviceList() {
return new ArrayList<>(mAapter.getBondedDevices());
}
}
3,蓝牙列表适配
package com.win16.bluetoothclass4;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
/** * Created by Rex on 2015/5/27. */
public class DeviceAdapter extends BaseAdapter {
private List<BluetoothDevice> mData;
private Context mContext;
public DeviceAdapter(List<BluetoothDevice> data, Context context) {
mData = data;
mContext = context.getApplicationContext();
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int i) {
return mData.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View itemView = view;
//复用View,优化性能
if( itemView == null) {
itemView = LayoutInflater.from(mContext).inflate(android.R.layout.simple_list_item_2,viewGroup,false);
}
TextView line1 = (TextView) itemView.findViewById(android.R.id.text1);
TextView line2 = (TextView) itemView.findViewById(android.R.id.text2);
//获取对应的蓝牙设备
BluetoothDevice device = (BluetoothDevice) getItem(i);
//显示名称
line1.setText(device.getName());
//显示地址
line2.setText(device.getAddress());
return itemView;
}
public void refresh(List<BluetoothDevice> data) {
mData = data;
notifyDataSetChanged();
}
}
4,界面展示
package com.win16.bluetoothclass4;
import android.annotation.TargetApi;
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.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.win16.bluetoothclass4.connect.AcceptThread;
import com.win16.bluetoothclass4.connect.ConnectThread;
import com.win16.bluetoothclass4.connect.Constant;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
/** * Created by Rex on 2015/5/27. */
public class MainActivity extends Activity {
public static final int REQUEST_CODE = 0;
private List<BluetoothDevice> mDeviceList = new ArrayList<>();
private List<BluetoothDevice> mBondedDeviceList = new ArrayList<>();
private BlueToothController mController = new BlueToothController();
private ListView mListView;
private DeviceAdapter mAdapter;
private Toast mToast;
private AcceptThread mAcceptThread;
private ConnectThread mConnectThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initActionBar();
setContentView(R.layout.activity_main);
initUI();
registerBluetoothReceiver();
mController.turnOnBlueTooth(this, REQUEST_CODE);
}
private void registerBluetoothReceiver() {
IntentFilter filter = new IntentFilter();
//开始查找
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
//结束查找
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
//查找设备
filter.addAction(BluetoothDevice.ACTION_FOUND);
//设备扫描模式改变
filter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
//绑定状态
filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(mReceiver, filter);
}
private Handler mUIHandler = new MyHandler();
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if( BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action) ) {
setProgressBarIndeterminateVisibility(true);
//初始化数据列表
mDeviceList.clear();
mAdapter.notifyDataSetChanged();
}
else if( BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
setProgressBarIndeterminateVisibility(false);
}
else if( BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//找到一个,添加一个
mDeviceList.add(device);
mAdapter.notifyDataSetChanged();
}
else if( BluetoothAdapter.ACTION_SCAN_MODE_CHANGED.equals(action)) {
int scanMode = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE,0);
if( scanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
setProgressBarIndeterminateVisibility(true);
}
else {
setProgressBarIndeterminateVisibility(false);
}
}
else if( BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action) ) {
BluetoothDevice remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if( remoteDevice == null ) {
showToast("no device");
return;
}
int status = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,0);
if( status == BluetoothDevice.BOND_BONDED) {
showToast("Bonded " + remoteDevice.getName());
}
else if( status == BluetoothDevice.BOND_BONDING){
showToast("Bonding " + remoteDevice.getName());
}
else if(status == BluetoothDevice.BOND_NONE){
showToast("Not bond " + remoteDevice.getName());
}
}
}
};
private void initUI() {
mListView = (ListView) findViewById(R.id.device_list);
mAdapter = new DeviceAdapter(mDeviceList, this);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(bindDeviceClick);
}
@Override
protected void onDestroy() {
super.onDestroy();
if( mAcceptThread != null) {
mAcceptThread.cancel();
}
if( mConnectThread != null) {
mConnectThread.cancel();
}
unregisterReceiver(mReceiver);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
private void showToast(String text) {
if( mToast == null) {
mToast = Toast.makeText(this, text, Toast.LENGTH_LONG);
}
else {
mToast.setText(text);
}
mToast.show();
}
@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();
//noinspection SimplifiableIfStatement
if (id == R.id.enable_visiblity) {
mController.enableVisibly(this);
}
else if( id == R.id.find_device) {
//查找设备
mAdapter.refresh(mDeviceList);
mController.findDevice();
mListView.setOnItemClickListener(bindDeviceClick);
}
else if (id == R.id.bonded_device) {
//查看已绑定设备
mBondedDeviceList = mController.getBondedDeviceList();
mAdapter.refresh(mBondedDeviceList);
mListView.setOnItemClickListener(bindedDeviceClick);
}
else if( id == R.id.listening) {
if( mAcceptThread != null) {
mAcceptThread.cancel();
}
mAcceptThread = new AcceptThread(mController.getAdapter(), mUIHandler);
mAcceptThread.start();
}
else if( id == R.id.stop_listening) {
if( mAcceptThread != null) {
mAcceptThread.cancel();
}
}
else if( id == R.id.disconnect) {
if( mConnectThread != null) {
mConnectThread.cancel();
}
}
else if( id == R.id.say_hello) {
say("Hello");
}
else if( id == R.id.say_hi) {
say("Hi");
}
return super.onOptionsItemSelected(item);
}
private void say(String word) {
if( mAcceptThread != null) {
try {
mAcceptThread.sendData(word.getBytes("utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
else if( mConnectThread != null) {
try {
mConnectThread.sendData(word.getBytes("utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
private AdapterView.OnItemClickListener bindDeviceClick = new AdapterView.OnItemClickListener() {
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
BluetoothDevice device = mDeviceList.get(i);
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
device.createBond();
}
}
};
private AdapterView.OnItemClickListener bindedDeviceClick = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
BluetoothDevice device = mBondedDeviceList.get(i);
if( mConnectThread != null) {
mConnectThread.cancel();
}
mConnectThread = new ConnectThread(device, mController.getAdapter(), mUIHandler);
mConnectThread.start();
}
};
private void initActionBar() {
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
getActionBar().setDisplayUseLogoEnabled(false);
setProgressBarIndeterminate(true);
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class
.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case Constant.MSG_START_LISTENING:
setProgressBarIndeterminateVisibility(true);
break;
case Constant.MSG_FINISH_LISTENING:
setProgressBarIndeterminateVisibility(false);
break;
case Constant.MSG_GOT_DATA:
showToast("data: "+String.valueOf(msg.obj));
break;
case Constant.MSG_ERROR:
showToast("error: "+String.valueOf(msg.obj));
break;
case Constant.MSG_CONNECTED_TO_SERVER:
showToast("Connected to Server");
break;
case Constant.MSG_GOT_A_CLINET:
showToast("Got a Client");
break;
}
}
}
}
5,界面布局
<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ListView android:id="@+id/device_list" android:layout_width="match_parent" android:layout_height="match_parent"></ListView>
</RelativeLayout>
6,源码下载
http://download.csdn.net/detail/eandroidhu/9433848