在Eclipse模拟器错误 [英] error on emulator of eclipse
问题描述
我收到以下错误的emulator.I'm发帖codeS这是与配对的蓝牙device.I'm通信能够连接蓝牙设备,其非Android的,但是当我添加$ C $下通讯(读/写),它提供了对emulator.Please错误提示,如果有人知道solution.I'm新的Android系统。
不幸的是,应用程序已经停止
MainActivity.java
公共类MainActivity扩展活动
{
按钮scanButton,connectButton,sendButton;
的EditText命令;
TextView中出;
UUID UUID = UUID.fromString(00001101-0000-1000-8000-00805F9B34FB); //标准SerialPortService ID
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
私有静态最终诠释REQUEST_ENABLE_BT = 0;
私人设置< BluetoothDevice类> pairedDevices;
公共静态的ArrayList<对象> BondedDeviceList;
的BluetoothSocket mmSocket,毫米;
BluetoothDevice类mmDevice;
的OutputStream mmOutputStream;
InputStream的mmInputStream;
//私有静态字符串地址=00:06:66:04:62:73;
@覆盖
保护无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(com.example.bluecomm.R.layout.activity_list_item);
扫描();
}
公共无效扫描()
{
scanButton =新的按钮(这一点);
scanButton =(按钮)findViewById(R.id.button1);
scanButton.setOnClickListener(新Button.OnClickListener()
{
@覆盖
公共无效的onClick(视图v)
{
如果(mBluetoothAdapter == NULL)
{
吐司面包= Toast.makeText(getApplicationContext(),不支持的设备,Toast.LENGTH_SHORT);
toast.show();
}
其他
{
如果(!mBluetoothAdapter.isEnabled())
{
吐司面包= Toast.makeText(getApplicationContext(),让您的设备后启用,Toast.LENGTH_SHORT);
toast.show();
意图enableBtIntent =新的意图(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent,REQUEST_ENABLE_BT);
}其他{}
pairedDevices = mBluetoothAdapter.getBondedDevices();
如果(pairedDevices == NULL)
{
//没有绑定设备
吐司面包= Toast.makeText(getApplicationContext(),没有绑定设备,Toast.LENGTH_SHORT);
toast.show();
}
其他
{
如果(pairedDevices.size()大于0)
{
//循环配对设备
对于(BluetoothDevice类设备:pairedDevices)
{
如果(device.getName()。等于(RN_BS-6273))
{
mmDevice =设备;
吐司面包= Toast.makeText(getApplicationContext(),设备发现,Toast.LENGTH_SHORT);
toast.show();
打破;
}其他{}
}
}其他{}
}
}
}
});
connectdevice();
}
公共无效connectdevice()
{
connectButton =新的按钮(这一点);
connectButton =(按钮)findViewById(R.id.button2);
connectButton.setOnClickListener(新Button.OnClickListener()
{
@覆盖
公共无效的onClick(视图查看)
{
尝试
{
MM = mmDevice.createRfcommSocketToServiceRecord(UUID);
吐司toast1 = Toast.makeText(getApplicationContext(),UUID分配,Toast.LENGTH_LONG);
toast1.show();
}
赶上(IOException异常E)
{}
mmSocket =毫米;
跑();
}
公共无效的run()
{
尝试
{
mmSocket.connect();
// mmOutputStream = mmSocket.getOutputStream();
// mmInputStream = mmSocket.getInputStream();
如果(mmSocket.isConnected())
{
吐司toast1 = Toast.makeText(getApplicationContext(),设备已连接,Toast.LENGTH_LONG);
toast1.show();
}
其他{}
/* 尝试
{
INT方bytesAvailable = mmInputStream.available();
如果(参考bytesAvailable大于0)
{
byte []的packetBytes =新的字节[方bytesAvailable]
mmInputStream.read(packetBytes);
}其他{}*/
}
赶上(IOException异常closeException)
{}
返回;
}
});
发送();
}
公共无效的send()
{
命令=(EditText上)findViewById(R.id.icon);
sendButton =新的按钮(这一点);
sendButton =(按钮)findViewById(R.id.button3);
sendButton.setOnClickListener(新Button.OnClickListener()
{
@覆盖
公共无效的onClick(视图查看)
{
InputStream的tmpIn = NULL;
OutputStream的tmpOut = NULL;
//获取输入和输出流,因为使用临时对象
//成员流是最终的
尝试 {
tmpIn = mmSocket.getInputStream();
tmpOut = mmSocket.getOutputStream();
}赶上(IOException异常E){}
mmInputStream = tmpIn;
mmOutputStream = tmpOut;
写();
}
公共无效的write()
{
尝试
{
弦乐味精= commands.getText()的toString()。
味精+ =\ N的;
mmOutputStream.write(msg.getBytes());
// mmInputStream.read();
}
赶上(IOException异常接近)
{
尝试 {
mmSocket.close();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
吐司toast1 = Toast.makeText(getApplicationContext(),套接字关闭......,Toast.LENGTH_LONG);
toast1.show();
}
返回;
}
});
}
}
activity_list_item.xml
< XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=MainActivity。>
<按钮
机器人:ID =@ + ID /按钮1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerHorizontal =真
机器人:文本=扫描/>
<按钮
机器人:ID =@ + ID /按钮2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerHorizontal =真
机器人:layout_centerVertical =真
机器人:layout_below =@ + ID /按钮1
机器人:文本=连接/>
<的EditText
机器人:ID =@ + ID /图标
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID /按钮2/>
<按钮
机器人:ID =@ + ID /按钮3
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerHorizontal =真
机器人:layout_centerVertical =真
机器人:layout_below =@ + ID /图标
机器人:文=发送/>
<的TextView
机器人:ID =@ + ID /数据
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID /按钮3
机器人:文本=数据:/>
< / RelativeLayout的>
您不能使用BluetoothAdapter与仿真。检查这的职位。
理想的情况下尝试测试在真实设备上。
关于崩溃,这是更好地检查(使用的logcat)日志,让你得到一些细节的地方出现这种情况。
I'm getting following error on emulator.I'm posting codes which is for communicating with paired Bluetooth device.I'm able to connect the Bluetooth device which is non Android,but when I add code for communication (read/write) it gives an error on emulator.Please suggest if anybody knows the solution.I'm new in 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>
You cannot use a BluetoothAdapter with an emulator. Check this post.
Ideally try to test on a real device.
Concerning the crash, it's better to check the logs (using logcat) so that you get some details where this happens.
这篇关于在Eclipse模拟器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!