我们能够在文本视图中列出配对的蓝牙设备。该应用程序有两个EditText,一个接受字符串消息,另一个接受目标设备的MAC地址,另一个发送按钮将消息发送到目的地地址,但点击发送按钮时应用程序将关闭。请帮助我们。 提前致谢。 package com.example.javapoint;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
TextView textview1;
UUID applicationUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private static final int REQUEST_ENABLE_BT = 1;
BluetoothAdapter btAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview1 = (TextView)findViewById(R.id.textView1);
// Getting the Bluetooth adapter
btAdapter = BluetoothAdapter.getDefaultAdapter();
textview1.append("\nAdapter: " + btAdapter);
CheckBluetoothState();
}
/* It is called when an activity completes.*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_ENABLE_BT) {
CheckBluetoothState();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
private void CheckBluetoothState() {
// Checks for the Bluetooth support and then makes sure it is turned on
// If it isn't turned on, request to turn it on
// List paired devices
if(btAdapter==null) {
textview1.append("\nBluetooth NOT supported. Aborting.");
return;
} else {
if (btAdapter.isEnabled()) {
textview1.append("\nBluetooth is enabled...");
// Listing paired devices
textview1.append("\nPaired Devices are:");
Set<BluetoothDevice> devices = btAdapter.getBondedDevices();
for (BluetoothDevice device : devices) {
textview1.append("\n Device: " + device.getName() + ", " + device);
}
} else {
//Prompt user to turn on Bluetooth
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
Button sbtn=(Button)findViewById(R.id.btn2);
final EditText et=(EditText)findViewById(R.id.message);
final String msg=et.getText().toString();
final EditText et1=(EditText)findViewById(R.id.macaddress);
final String mac=et1.getText().toString();
sbtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
byte[] toSend = msg.getBytes();
try {
BluetoothDevice mBluetoothDevice = btAdapter.getRemoteDevice(mac);
BluetoothSocket socket =mBluetoothDevice.createInsecureRfcommSocketToServiceRecord(applicationUUID);
socket.connect();
OutputStream mmOutStream = socket.getOutputStream();
mmOutStream.write(toSend);
socket.close();
// Your Data is sent to BT connected paired device ENJOY.
} catch (IOException e) {
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}