再次......
我正在尝试创建蓝牙连接,我已经拥有该设备的MAC地址,但是当我使用createInsecureRfcommSocketToServiceRecord时,它给了我一个关于UUID的错误......有没有人知道为什么会这样? ..我提前感谢你的帮助。
这是Main:
package com.example.mustangsound;
//import java.util.UUID;
import java.util.Set;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.IntentFilter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class MainActivity extends Activity {
String toastText = "";
private BluetoothDevice btDevice;
private BluetoothSocket btSocket;
private String btAddress = "11:22:33:44:44:44";
String mArrayAdapter = "";
private BluetoothAdapter mBluetoothAdapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void BTConnect (View v){
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
finish();
return;
}
else{
toastText="Connecting to Mustang Sound Bluetooth receiver.... please wait";
Toast.makeText(MainActivity.this, toastText, Toast.LENGTH_SHORT).show();
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(btAddress);
btSocket = device.createInsecureRfcommSocketToServiceRecord(UUID.fromString("0000111F-0000-1000-8000-00805F9B34FB"));
btSocket.connect();
}
}
public void LightsOn (View v){
toastText="Sounds Lights On";
Toast.makeText(MainActivity.this, toastText, Toast.LENGTH_SHORT).show();
}
public void LightsOff (View v){
toastText="Sounds Lights Off";
Toast.makeText(MainActivity.this, toastText, Toast.LENGTH_SHORT).show();
}
public void DoorsOpen (View v){
toastText="Opening Sound doors";
Toast.makeText(MainActivity.this, toastText, Toast.LENGTH_SHORT).show();
}
public void DoorsClose (View v){
toastText="Closing Sound doors";
Toast.makeText(MainActivity.this, toastText, Toast.LENGTH_SHORT).show();
}
public void SysOn (View v){
toastText="IT'S ALIVE!!!!!.....IT'S ALIIIIIVEEEEEE";
Toast.makeText(MainActivity.this, toastText, Toast.LENGTH_SHORT).show();
}
public void SysOff (View v){
toastText="Mustnag Sound OFF";
Toast.makeText(MainActivity.this, toastText, Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:0)
似乎AndroidStudio / IntelliJ在某处有错误,因此未添加java util jar来编译classpath。具有讽刺意味的是 - 键入import
语句时自动完成功能正常。很奇怪,在你的情况下 - 对于java.util.Set
它已经解决了。 Set
可能已在其他地方提供,而我java.util.Date
也有同样的问题。
对我来说,这是一个全新的项目,但解决方案是退出AndroidStudio,删除.iml
文件和.idea
目录并重新打开项目。从gradle重新同步后 - 一切都很好。