android:id="@+id/tv_2"
android:layout_hljs-string">"fill_parent"
android:layout_hljs-string">"wrap_content"
android:layout_below="@+id/listView_bound"
android:layout_marginTop="25dp"
android:layout_marginBottom="5dp"
android:text="可用设备"
/>
android:id="@+id/listView_isConnect"
android:layout_below="@+id/tv_2"
android:layout_hljs-string">"wrap_content"
android:layout_hljs-string">"wrap_content"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
/>
<TextView
android:id="@+id/Address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
/>
LinearLayout>
package com.zw.findbluetest;
import android.support.v7.app.ActionBarActivity;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
public BluetoothAdapter mBluetoothAdapter;
private BluetoothSocket btSocket;
private ConnectThread connectThread;
private LeDeviceListAdapter mLeDeviceListAdapter_dound;
private LeDeviceListAdapter mLeDeviceListAdapter_isConnect;
private ListView bleDev_lvBound;
private ListView bleDev_lvConnect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
mLeDeviceListAdapter_dound = new LeDeviceListAdapter(this);
mLeDeviceListAdapter_isCOnnect= new LeDeviceListAdapter(this);
bleDev_lvBound = (ListView) findViewById(R.id.listView_bound);
bleDev_lvBound.setAdapter(mLeDeviceListAdapter_dound);
bleDev_lvCOnnect= (ListView) findViewById(R.id.listView_isConnect);
bleDev_lvConnect.setAdapter(mLeDeviceListAdapter_isConnect);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set devices = mBluetoothAdapter.getBondedDevices();
if (devices.size() > 0) {
for (BluetoothDevice bluetoothDevice : devices) {
mLeDeviceListAdapter_dound.addDevice(bluetoothDevice);
mLeDeviceListAdapter_dound.notifyDataSetChanged();
}
}
/**
* 已配对listView的点击事件
*/
bleDev_lvBound.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
BluetoothDevice device = mLeDeviceListAdapter_dound.getDevice(position);
cOnnectThread= new ConnectThread(device);
System.out.println("连接中....");
connectThread.start();
Toast.makeText(MainActivity.this, "点击设备是"+ device.getName() + " " + device.getAddress(), Toast.LENGTH_LONG).show() ;
}
}) ;
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);
/**
* 可用listView的点击事件
*/
bleDev_lvConnect.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
try {
BluetoothDevice device = mLeDeviceListAdapter_isConnect.getDevice(position);
Boolean returnValue = false;
Method createBondMethod;
if(device.getBondState() == BluetoothDevice.BOND_NONE) {
createBOndMethod= BluetoothDevice.class .getMethod("createBond");
System.out.println("开始配对");
returnValue = (Boolean) createBondMethod.invoke(device);
Toast.makeText(MainActivity.this, "点击设备是"+ device.getName() + " " + device.getAddress(), Toast.LENGTH_LONG).show() ;
}else if(device.getBondState() == BluetoothDevice.BOND_BONDED){
cOnnectThread= new ConnectThread(device);
connectThread.start();
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
});
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
System.out.println(action);
if (action.equals(BluetoothDevice.ACTION_FOUND)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
mLeDeviceListAdapter_isConnect.addDevice(device);
mLeDeviceListAdapter_isConnect.notifyDataSetChanged();
}
} else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
setProgressBarIndeterminateVisibility(false);
setTitle("搜索蓝牙设备");
}
}
};
public void openBlue(View v){
if(mBluetoothAdapter == null){
Toast.makeText(this,"本地蓝牙不可用",Toast.LENGTH_SHORT).show();
finish();
}
if(!mBluetoothAdapter.isEnabled()){
mBluetoothAdapter.enable();
Toast.makeText(this, "正在打开..", 0).show();
}
Intent enable = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
enable.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 3600);
startActivity(enable);
Intent searchIntent = new Intent(this, MainActivity.class);
startActivity(searchIntent);
}
public void closeBlue(View v){
if(mBluetoothAdapter.isEnabled()){
mBluetoothAdapter.disable();
Toast.makeText(this, "正在关闭..", 0).show();
}else{
Toast.makeText(getApplicationContext(), "蓝牙未打开,先打开蓝牙", 0).show();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void findBlue(View v) {
setProgressBarIndeterminateVisibility(true);
setTitle("正在扫描....");
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
mBluetoothAdapter.startDiscovery();
}
}
package com.zw.findbluetest;
import java.util.ArrayList;
import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
/**
* 用于listView的配置;
* @author wen
*
*/
public class LeDeviceListAdapter extends BaseAdapter {
private ArrayList mLeDevices;
private LayoutInflater mInflator;
private Activity mContext;
public LeDeviceListAdapter(Activity c) {
super();
mCOntext= c;
mLeDevices = new ArrayList();
mInflator = mContext.getLayoutInflater();
}
public void addDevice(BluetoothDevice device) {
if (!mLeDevices.contains(device)) {
mLeDevices.add(device);
System.out.println(device.getName() + " " + device.getAddress());
}
}
public BluetoothDevice getDevice(int position) {
return mLeDevices.get(position);
}
public void clear() {
mLeDevices.clear();
}
@Override
public int getCount() {
return mLeDevices.size();
}
@Override
public Object getItem(int position) {
return mLeDevices.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
ViewHolder viewHolder;
if (view == null) {
view = mInflator.inflate(R.layout.item, null);
viewHolder = new ViewHolder();
viewHolder.deviceAddress = (TextView) view.findViewById(R.id.Address);
viewHolder.deviceName = (TextView) view.findViewById(R.id.Name);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
BluetoothDevice device = mLeDevices.get(position);
final String deviceName = device.getName();
if (deviceName != null && deviceName.length() > 0) {
viewHolder.deviceName.setText(deviceName);
} else {
viewHolder.deviceName.setText("未知设备");
}
viewHolder.deviceAddress.setText(device.getAddress());
return view;
}
final class ViewHolder {
TextView deviceName;
TextView deviceAddress;
}
}