所以我知道此类问题已经发布过,但我无法理解它们,我目前有一个简单的程序显示配对设备(工作正常)然后当你按下按钮时它应该发送数据到另一个设备的TextView来编辑它。这是我的代码,任何人都可以看到我做错了什么?我还是Android Studio的新手。
public class MainActivity extends AppCompatActivity {
private ConnectedThread connectedThread;
private BluetoothAdapter btAdapter;
private BluetoothDevice btDevice;
private BluetoothSocket btSocket;
private ParcelUuid[] uuids;
private OutputStream outStream;
private InputStream inStream;
private Button btnShowPaired;
private ListView lstPaired;
private Button btnSendData;
private TextView txtViewData;
private ArrayAdapter<String> arrayAdapter;
private Set<BluetoothDevice> setPaired;
private ArrayList<String> aryLstPairedName;
private final int MESSAGE_READ = 0;
private byte[] buffer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connectedThread = new ConnectedThread();
btAdapter = BluetoothAdapter.getDefaultAdapter();
btnShowPaired = (Button) findViewById(R.id.btnShowPaired);
lstPaired = (ListView) findViewById(R.id.lstPaired);
btnSendData = (Button) findViewById(R.id.btnSendData);
txtViewData = (TextView) findViewById(R.id.txtViewData);
btnShowPaired.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setPaired = (Set)btAdapter.getBondedDevices();
UpdateNameList();
arrayAdapter.notifyDataSetChanged();
}
});
lstPaired.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
List<BluetoothDevice> btList = new ArrayList<>(setPaired);
btDevice = btList.get(position);
try {
SetupTransfer();
} catch (IOException e) {
}
}
});
btnSendData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
setPaired = new HashSet<BluetoothDevice>();
}
public void UpdateNameList()
{
aryLstPairedName = new ArrayList<String>();
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, aryLstPairedName);
lstPaired.setAdapter(arrayAdapter);
for(BluetoothDevice device : setPaired)
{
aryLstPairedName.add(device.getName());
}
}
public void SetupTransfer() throws IOException {
ParcelUuid[] uuids = btDevice.getUuids();
BluetoothSocket btSocket = btDevice.createRfcommSocketToServiceRecord(uuids[0].getUuid());
btSocket.connect();
outStream = btSocket.getOutputStream();
inStream = btSocket.getInputStream();
connectedThread = new ConnectedThread();
connectedThread.start();
}
public class ConnectedThread extends Thread
{
public ConnectedThread()
{
}
public void Read() throws IOException {
byte[] buffer = new byte[1024];
int bytes;
while(true)
{
try {
bytes = inStream.read(buffer);
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
} catch (IOException e) {
break;
}
}
}
public void Write(String s) throws IOException
{
outStream.write(s.getBytes());
}
}
private final Handler mHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case MESSAGE_READ:
byte[] readBuffer = (byte[]) msg.obj;
String readMessage = new String(readBuffer, 0, msg.arg1);
txtViewData.append(readMessage);
}
}
};
}