activity
package com.example.day10_2;
import android.Manifest;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import com.example.day10.R;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
List<BluetoothDevice> list;
MyAdapter myAdapter;
BluetoothAdapter bluetoothAdapter;
BluetoothManager bluetoothManager;
BluetoothReceiver bluetoothReceiver;
Button start, stop, search;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
request();
initDate();
}
public void initDate() {
bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
if (bluetoothAdapter == null) {
finish();
return;
}
bluetoothReceiver = new BluetoothReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(bluetoothReceiver, intentFilter);
}
public void initView() {
start = findViewById(R.id.start);
stop = findViewById(R.id.stop);
search = findViewById(R.id.serach);
lv = findViewById(R.id.list);
list = new ArrayList<>();
start.setOnClickListener(this);
stop.setOnClickListener(this);
search.setOnClickListener(this);
myAdapter = new MyAdapter(list);
lv.setAdapter(myAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try {
Method method = BluetoothDevice.class.getMethod("createBond");
method.invoke(list.get(position));
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void request() {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
int i = checkSelfPermission(Manifest.permission.BLUETOOTH);
int i2 = checkSelfPermission(Manifest.permission.BLUETOOTH_ADMIN);
int i3 = checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION);
int granted = PackageManager.PERMISSION_GRANTED;
if (i != granted || i2 != granted || granted != i3) {
String[] permiss = {Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.ACCESS_COARSE_LOCATION};
requestPermissions(permiss, 100);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 100) {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
}
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start:
boolean enable = bluetoothAdapter.enable();
if (!enable) {
Intent intent = new Intent();
intent.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);
intent.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 200);
}
break;
case R.id.stop:
bluetoothAdapter.disable();
list.clear();
myAdapter.notifyDataSetChanged();
break;
case R.id.serach:
bluetoothAdapter.startDiscovery();
break;
}
}
public class BluetoothReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int state = bluetoothDevice.getBondState();
if (state == BluetoothDevice.BOND_BONDED) {
Log.e("###", "连接成功");
}
break;
case BluetoothDevice.ACTION_FOUND:
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
list.add(device);
myAdapter.notifyDataSetChanged();
break;
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
myAdapter.notifyDataSetChanged();
break;
}
}
}
}
ListView适配器
package com.example.day10_2;
import android.bluetooth.BluetoothDevice;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.day10.R;
import java.util.ArrayList;
import java.util.List;
class MyAdapter extends BaseAdapter {
List<BluetoothDevice> list;
public MyAdapter(List<BluetoothDevice> list) {
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = View.inflate(parent.getContext(), R.layout.list, null);
viewHolder.name = convertView.findViewById(R.id.name);
viewHolder.address = convertView.findViewById(R.id.address);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.name.setText(list.get(position).getName());
viewHolder.address.setText(list.get(position).getAddress());
}
return convertView;
}
public class ViewHolder {
TextView name;
TextView address;
}
}