我试图给我的应用程序提供与新设备绑定的功能,但我的代码不起作用,我没有任何崩溃
package com.kyuukun.boltcontroller.Command;
import android.app.Activity;
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.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.UUID;
public class DataControl
{
private BluetoothAdapter btadapter;
private ArrayList paired = new ArrayList();;
private BluetoothDevice connected;
private BluetoothSocket socket;
private BroadcastReceiver receiver;
public DataControl(BluetoothAdapter adapter)
{
this.btadapter = adapter;
}
public BluetoothAdapter getAdapter()
{
return btadapter;
}
public ArrayList getPaired()
{
return paired;
}
public void startPaired(Activity act)
{
if(!btadapter.getBondedDevices().isEmpty())
{
for(BluetoothDevice d : btadapter.getBondedDevices())
{
paired.add(d);
}
}
btadapter.startDiscovery();
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
BroadcastReceiver r = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
Log.d("SENPAI", action);
if (BluetoothDevice.ACTION_FOUND.equals(action))
{
BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
paired.add(device);
} else {}
}
};
act.registerReceiver(r, filter);
receiver = r;
}
public boolean startConnection(BluetoothDevice device)
{
boolean c = false;
if(paired.contains(device))
{
BluetoothSocket sck;
try {
sck = device.createRfcommSocketToServiceRecord(UUID.randomUUID());
sck.connect();
this.socket = sck;
this.connected = device;
c = true;
} catch (IOException e) {
e.printStackTrace();
}
}
return c;
}
public BroadcastReceiver getReceiver()
{
return receiver;
}
public boolean send(String s)
{
boolean sd = false;
if(connected != null && socket != null)
{
try {
socket.getOutputStream().write(s.getBytes());
sd = true;
} catch (IOException e) {
e.printStackTrace();
sd = false;
}
}
return sd;
}
public String getData()
{
String s = "";
if(connected != null && socket != null)
{
InputStream stream;
try {
stream = socket.getInputStream();
if(stream.available() > 0)
{
byte[] b = new byte[stream.available() + 1];
stream.read(b);
String newstring = new String(b);
s.concat(newstring);
}
} catch (IOException e) {
e.printStackTrace();
}
}
return s;
}
}
活动代码:
MainActivity.control.startPaired(LoadingActivity.this);
if(MainActivity.control.getPaired().size() > 0)
{
MainActivity.control.getAdapter().cancelDiscovery();
Intent intent = new Intent(LoadingActivity.this, PairedDevicesActivity.class);
startActivity(intent);
finish();
break;
}
我在日志上收到的信息:
08-27 22:38:36.049 26687-26687 / com.kyuukun.boltcontroller D / SENPAI:android.bluetooth.adapter.action.DISCOVERY_STARTED
当我得到绑定的divices它工作正常,但当我尝试获取新设备时,它什么都不做。