我创建了一个用于蓝牙准备的活动,并为蓝牙功能嵌入了一个片段。 所有必需的权限,如精细位置和粗略位置都添加到androidmanifest文件。这里没有添加xml文件,因为没有占用太多空间。发现开始后我查看了日志,但没有看到日志中的附近设备。我不知道出了什么问题。
MyActivity.java
public class MainActivity extends AppCompatActivity{
BluetoothAdapter bluetoothAdapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
new AlertDialog.Builder(this)
.setTitle("Not Compatible")
.setMessage("Your phone does not support Bluetooth")
.setPositiveButton("Exit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
}).show();
}
if (!bluetoothAdapter.isEnabled()) {
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 1);
}
ScanFragment scanFragment = ScanFragment.newInstance(bluetoothAdapter);
getSupportFragmentManager().beginTransaction().add(R.id.frame_container, scanFragment).commit();
}
ScanFragment
public class ScanFragment extends android.support.v4.app.Fragment implements View.OnClickListener {
private List<DeviceItem> deviceItems;
private Set<BluetoothDevice> bondedDevices;
private List<BroadcastReceiver> receivers;
private static BluetoothAdapter bluetoothAdapter;
private Button scanButton;
private ArrayAdapter<DeviceItem> mAdapter;
public static ScanFragment newInstance(BluetoothAdapter bluetoothAdapter) {
ScanFragment fragment = new ScanFragment();
ScanFragment.bluetoothAdapter = bluetoothAdapter;
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
deviceItems = new ArrayList<>();
receivers = new ArrayList<>();
bondedDevices = bluetoothAdapter.getBondedDevices();
mAdapter = new ArrayAdapter<DeviceItem>(this.getContext(), android.R.layout.simple_list_item_1, android.R.id.text1);
if (bondedDevices.size() > 0) {
System.out.println("ScanFragment " + "pairded devices are below");
for (BluetoothDevice device : bondedDevices) {
DeviceItem newDevice = new DeviceItem(device.getName(), device.getAddress(), "false");
deviceItems.add(newDevice);
System.out.println("ScanFragment pairded deviceName" + newDevice.getDeviceName());
System.out.println("ScanFragment pairded deviceAddress " + newDevice.getAddress());
}
}
}
@Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstanceState) {
View view = layoutInflater.inflate(R.layout.scan_fragment, viewGroup, false);
scanButton = (Button) view.findViewById(R.id.button_scan);
scanButton.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
if (v == scanButton) {
if (bluetoothAdapter.isDiscovering()) {
bluetoothAdapter.cancelDiscovery();
}
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
getActivity().registerReceiver(broadcastReceiver, intentFilter);
receivers.add(broadcastReceiver);
checkBluetoothPermission();
bluetoothAdapter.startDiscovery();
}
}
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
System.out.println("ScanFragment " + "scan started");
break;
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
System.out.println("ScanFragment " + "scan finished");
break;
case BluetoothDevice.ACTION_FOUND:
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
DeviceItem newDevice = new DeviceItem(device.getName(), device.getAddress(), "false");
System.out.println("ScanFragment " + "device found " + device.getName());
mAdapter.add(newDevice);
break;
}
}
};
@Override
public void onDestroy() {
super.onDestroy();
if (receivers.contains(broadcastReceiver)) {
getActivity().unregisterReceiver(broadcastReceiver);
}
if (bluetoothAdapter.isDiscovering()) {
bluetoothAdapter.cancelDiscovery();
System.out.println("ScanFragment " + "device discovery cancelled");
}
}
private void checkBluetoothPermission() {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
int permission_check = getActivity().checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCAITON");
permission_check += getActivity().checkSelfPermission("Manifest.permission.ACCESS_COARSE_LOCATION");
if (permission_check != 0) {
getActivity().requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001);
}
}
}
DeviceItem
public class DeviceItem {
private String deviceName;
private String address;
private boolean connected;
public String getDeviceName() {
return deviceName;
}
public boolean getConnected() {
return connected;
}
public String getAddress() {
return address;
}
public void setDeviceName(String deviceName) {
this.deviceName = deviceName;
}
public DeviceItem(String name, String address, String connected){
this.deviceName = name;
this.address = address;
if (connected == "true") {
this.connected = true;
}
else {
this.connected = false;
}
}