我在模拟器上遇到以下错误。我正在发布用于与配对蓝牙设备通信的代码。我能够连接非 Android 的蓝牙设备,但是当我添加通信代码(读/写)时在模拟器上出现错误。请建议是否有人知道解决方案。我是 Android 新手。
"Unfortunately, App has stopped "
MainActivity.java
public class MainActivity extends Activity
{
Button scanButton,connectButton,sendButton;
EditText commands;
TextView out;
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //Standard SerialPortService ID
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private static final int REQUEST_ENABLE_BT=0;
private Set<BluetoothDevice> pairedDevices;
public static ArrayList<Object> BondedDeviceList;
BluetoothSocket mmSocket,mm;
BluetoothDevice mmDevice;
OutputStream mmOutputStream;
InputStream mmInputStream;
//private static String address = "00:06:66:04:62:73";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(com.example.bluecomm.R.layout.activity_list_item);
scan();
}
public void scan()
{
scanButton = new Button(this);
scanButton = (Button)findViewById(R.id.button1);
scanButton.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
if (mBluetoothAdapter == null)
{
Toast toast = Toast.makeText(getApplicationContext(), "device not supported", Toast.LENGTH_SHORT);
toast.show();
}
else
{
if (!mBluetoothAdapter.isEnabled())
{
Toast toast = Toast.makeText(getApplicationContext(), "MAKING YOUR DEVICE ENABLE", Toast.LENGTH_SHORT);
toast.show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}else{}
pairedDevices = mBluetoothAdapter.getBondedDevices();
if(pairedDevices==null)
{
//No Bonded Devices
Toast toast = Toast.makeText(getApplicationContext(), "No Bonded Devices", Toast.LENGTH_SHORT);
toast.show();
}
else
{
if (pairedDevices.size() > 0)
{
// Loop through paired devices
for (BluetoothDevice device : pairedDevices)
{
if(device.getName().equals("RN_BS-6273"))
{
mmDevice = device;
Toast toast = Toast.makeText(getApplicationContext(), "Device Found", Toast.LENGTH_SHORT);
toast.show();
break;
}else{}
}
}else{}
}
}
}
});
connectdevice();
}
public void connectdevice()
{
connectButton = new Button(this);
connectButton = (Button)findViewById(R.id.button2);
connectButton.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View view)
{
try
{
mm = mmDevice.createRfcommSocketToServiceRecord(uuid);
Toast toast1 = Toast.makeText(getApplicationContext(), "uuid assigned", Toast.LENGTH_LONG);
toast1.show();
}
catch(IOException e)
{}
mmSocket=mm;
run();
}
public void run()
{
try
{
mmSocket.connect();
// mmOutputStream = mmSocket.getOutputStream();
//mmInputStream = mmSocket.getInputStream();
if(mmSocket.isConnected())
{
Toast toast1 = Toast.makeText(getApplicationContext(), "device is connected", Toast.LENGTH_LONG);
toast1.show();
}
else{}
/* try
{
int bytesAvailable = mmInputStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
}else{}*/
}
catch (IOException closeException)
{ }
return;
}
});
send();
}
public void send()
{
commands=(EditText)findViewById(R.id.icon);
sendButton = new Button(this);
sendButton = (Button)findViewById(R.id.button3);
sendButton.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View view)
{
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = mmSocket.getInputStream();
tmpOut = mmSocket.getOutputStream();
} catch (IOException e) { }
mmInputStream = tmpIn;
mmOutputStream = tmpOut;
write();
}
public void write()
{
try
{
String msg = commands.getText().toString();
msg += "\n";
mmOutputStream.write(msg.getBytes());
// mmInputStream.read();
}
catch (IOException close)
{
try {
mmSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast toast1 = Toast.makeText(getApplicationContext(), "Socket closing....", Toast.LENGTH_LONG);
toast1.show();
}
return;
}
});
}
}
activity_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Scan" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_below="@+id/button1"
android:text="Connect" />
<EditText
android:id="@+id/icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_below="@+id/icon"
android:text="Send" />
<TextView
android:id="@+id/data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button3"
android:text="DATA:"/>
</RelativeLayout>