我正在用android Studio开发扫描android应用程序的蓝牙设备。有一个输入字段,当输入名称与扫描名称匹配时,我想将数据发送到firebase。下面我已经提到了我在做什么
public class MainActivity extends Activity {
private static final int REQUEST_ENABLE_BT = 1;
private static final int PERMISSIONS_REQUEST_CODE = 11;
ArrayList<Map<String, Integer>> deviceInfo = new ArrayList<>();
List<String> headerList = new ArrayList<>();
List<String> dataList = new ArrayList<>();
Snapobj snap ;
List<Snapobj > snapList = new ArrayList<Snapobj>();
public final int WRITE_PERMISSON_REQUEST_CODE =111;
ListView listDevicesFound;
Button btnScanDevice;
TextView stateBluetooth;
BluetoothAdapter bluetoothAdapter;
ArrayAdapter<String> btArrayAdapter;
FirebaseDatabase db = FirebaseDatabase.getInstance();
private DatabaseReference databaseRef;
private DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("BluetoothDevicesInfo");
List<DeviceInfo> deviceList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!havePermissions()) {
Log.d("TAG", "Requesting permissions needed for this app.");
requestPermissions();
}
setContentView(R.layout.activity_main);
btnScanDevice = (Button)findViewById(R.id.scandevice);
stateBluetooth = (TextView)findViewById(R.id.bluetoothstate);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
listDevicesFound = (ListView)findViewById(R.id.devicesfound);
btArrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1);
listDevicesFound.setAdapter(btArrayAdapter);
CheckBlueToothState();
deviceList = new ArrayList<>();
btnScanDevice.setOnClickListener(new View.OnClickListener(){
@Override
//On click function
public void onClick(View view) {
btArrayAdapter.clear();
bluetoothAdapter.startDiscovery();
}
});
registerReceiver(ActionFoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(ActionFoundReceiver);
}
private void CheckBlueToothState(){
if (bluetoothAdapter == null){
// stateBluetooth.setText("Bluetooth NOT support");
}else{
if (bluetoothAdapter.isEnabled()){
if(bluetoothAdapter.isDiscovering()){
stateBluetooth.setText("Bluetooth is currently in device discovery process.");
}else{
stateBluetooth.setText("Bluetooth is Enabled.");
btnScanDevice.setEnabled(true);
}
}else{
// stateBluetooth.setText("Bluetooth is NOT Enabled!");
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode == REQUEST_ENABLE_BT){
CheckBlueToothState();
}
}
private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
Map<String, Integer> rssiMapper = new HashMap<>();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
btArrayAdapter.add(device.getName() + "\nAddress : " + device.getAddress() + "\nRSSI : " + rssi+"\n");
btArrayAdapter.notifyDataSetChanged();
rssiMapper.put(device.getName(), rssi);
deviceInfo.add(rssiMapper);
System.out.println("hashmap : "+rssiMapper);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
EditText x = (EditText) findViewById(R.id.distance);
Double distanceVal = Double.valueOf(x.getText().toString());
EditText y = findViewById(R.id.deviceName);
String deviceName = y.getText().toString()+ " ";
if (device.getName().equals(deviceName+ " ")) {
databaseReference = db.getReference("BluetoothDevicesInfo");
DeviceInfo deviceInfoObj = new DeviceInfo(device.getAddress(), dateFormat.format(new Date()), rssi, distanceVal);
String id = databaseReference.push().getKey();
databaseReference.child(id).setValue(deviceInfoObj);
} else {
Toast.makeText(getApplicationContext(), "Couldn't find a devices that matches your input", Toast.LENGTH_SHORT).show();
}
}
}
};
private void saveValues(Map<String, Short> rssiMapper) {
Log.d("Output", rssiMapper.toString());
}
private boolean havePermissions() {
return ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED;
}
private void requestPermissions() {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSIONS_REQUEST_CODE);
Log.d("TAG", "requestPermissions");
}
}
当我运行应用程序时,应用程序已停止,出现如下错误
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.bluetooth.device.action.FOUND flg=0x10 (has extras) } in com.example.scanbt.MainActivity$2@9f5f3be
at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$-android_app_LoadedApk$ReceiverDispatcher$Args_52226(LoadedApk.java:1329)
at android.app.-$Lambda$FilBqgnXJrN9Mgyks1XHeAxzSTk.$m$0(Unknown Source:4)
at android.app.-$Lambda$FilBqgnXJrN9Mgyks1XHeAxzSTk.run(Unknown Source:0)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.NumberFormatException: For input string: "chathurika-HP"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:539)
at java.lang.Double.valueOf(Double.java:503)
我能做些什么来避免NumberFormatException
String deviceName = y.getText().toString() + "";
这就解决了你的问题。如果再次出现这种错误,请像这样更改try-catch块,
try {
}catch(Exception e){}
您也可以这样更改:
device.getName().equals(deviceName+"")
您好,我正在尝试将excel表数据保存到MySQL数据库中。为此,我使用Apache Poi。我在Apache Poi中使用DataForcat获取单元格值。我成功地将单元格值作为字符串格式获取,但我试图将字符串值更改为Long我正在获取NumberFormatException。我正在尝试下面的代码。请尝试解决我的问题。 非常感谢。 这是控制台中的错误堆栈跟踪。
我一直得到这个错误:(错误出现在Donasi.class 这是课堂上的Donasi: 这是类DonationAdapter: 这是我的日志: 如何解决这个问题?
运行代码时,我收到一个数字格式异常: 如何防止此异常发生?
这是我的错误行, 最终int plChildren=整数。parseInt(绘图仪子项); 那么为什么会出现这样的错误呢?
当我在Jsp程序中使用属性自定义标记时,出现了一些错误: Servlet。路径为[/Tag]的上下文中servlet[jsp]的service()引发了具有根本原因java的异常[无法编译jsp类]。lang.NumberFormatException:用于输入字符串:“标记” 细节如下: 标签jsp tag.tld 日期标签。JAVA
当我从一个单独的客户端程序接收到一个整数(事务ID)转换成字符串时,我的Java UDP服务器程序的一个片段出错: 客户: 服务器: 当客户端发送数据包时,在字符串到整数的转换过程中,我得到了以下错误: 我还使用了整数。parseInt(clientMsg)具有类似的结果。然而,只要我没有尝试将数据包转换成整数,程序就可以将数据包打印为字符串值。 此外,虽然它在终端中显示了这一点,但当我将此错误消