Java 多活动蓝牙

Java 多活动蓝牙,java,android,bluetooth,Java,Android,Bluetooth,我正试着用蓝牙连接几项活动 (DeviceList.java和LedControl.java的来源:) 这是我的密码: DeviceList.java public class DeviceList extends AppCompatActivity { Button btnPaired; ListView devicelist; private BluetoothAdapter myBluetooth = null; private Set<BluetoothDevice> p

我正试着用蓝牙连接几项活动

(DeviceList.java和LedControl.java的来源:)

这是我的密码:

DeviceList.java

public class DeviceList extends AppCompatActivity {

Button btnPaired;
ListView devicelist;

private BluetoothAdapter myBluetooth = null;
private Set<BluetoothDevice> pairedDevices;
public static String EXTRA_ADDRESS = "device_address";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_device_list);

    btnPaired = (Button) findViewById(R.id.button);
    devicelist = (ListView) findViewById(R.id.listView);

    myBluetooth = BluetoothAdapter.getDefaultAdapter();
    if ( myBluetooth==null ) {
        Toast.makeText(getApplicationContext(), "Bluetooth device not available", Toast.LENGTH_LONG).show();
        finish();
    } else if ( !myBluetooth.isEnabled() ) {
        Intent turnBTon = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(turnBTon, 1);
    }

    btnPaired.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pairedDevicesList();
        }
    });
}

private void pairedDevicesList () {
    pairedDevices = myBluetooth.getBondedDevices();
    ArrayList list = new ArrayList();

    if ( pairedDevices.size() > 0 ) {
        for ( BluetoothDevice bt : pairedDevices ) {
            list.add(bt.getName().toString() + "\n" + bt.getAddress().toString());
        }
    } else {
        Toast.makeText(getApplicationContext(), "No Paired Bluetooth Devices Found.", Toast.LENGTH_LONG).show();
    }

    final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
    devicelist.setAdapter(adapter);
    devicelist.setOnItemClickListener(myListClickListener);
}

private AdapterView.OnItemClickListener myListClickListener = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String info = ((TextView) view).getText().toString();
        String address = info.substring(info.length()-17);

        Intent i = new Intent(DeviceList.this, ledControl.class);
        i.putExtra(EXTRA_ADDRESS, address);
        startActivity(i);
    }
};
public class ledControl extends AppCompatActivity {

Button btn1, btn2, btn3, btn4, btn5, btnDis;
String address = null;
TextView lumn;
private ProgressDialog progress;
BluetoothAdapter myBluetooth = null;
BluetoothSocket btSocket = null;
private boolean isBtConnected = false;
static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent newint = getIntent();
    address = newint.getStringExtra(DeviceList.EXTRA_ADDRESS);

    setContentView(R.layout.activity_led_control);

    btn1 = (Button) findViewById(R.id.button2);
    btn2 = (Button) findViewById(R.id.button3);
    btn3 = (Button) findViewById(R.id.button5);
    btn4 = (Button) findViewById(R.id.button6);
    btn5 = (Button) findViewById(R.id.button7);
    btnDis = (Button) findViewById(R.id.button4);
    lumn = (TextView) findViewById(R.id.textView2);

    new ConnectBT().execute();


    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(ledControl.this, test.class);
            i.putExtra(DeviceList.EXTRA_ADDRESS, address);
            startActivity(i);
        }
    });

}

private void sendSignal ( String number ) {
    if ( btSocket != null ) {
        try {
            btSocket.getOutputStream().write(number.toString().getBytes());
        } catch (IOException e) {
            msg("Error");
        }
    }
}

private void Disconnect () {
    if ( btSocket!=null ) {
        try {
            btSocket.close();
        } catch(IOException e) {
            msg("Error");
        }
    }

    finish();
}

private void msg (String s) {
    Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}

private class ConnectBT extends AsyncTask<Void, Void, Void> {
    private boolean ConnectSuccess = true;

    @Override
    protected  void onPreExecute () {
        progress = ProgressDialog.show(ledControl.this, "Connecting...", "Please Wait!!!");
    }

    @Override
    protected Void doInBackground (Void... devices) {
        try {
            if ( btSocket==null || !isBtConnected ) {
                myBluetooth = BluetoothAdapter.getDefaultAdapter();
                BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(address);
                btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);
                BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
                btSocket.connect();
            }
        } catch (IOException e) {
            ConnectSuccess = false;
        }

        return null;
    }

    @Override
    protected void onPostExecute (Void result) {
        super.onPostExecute(result);

        if (!ConnectSuccess) {
            msg("Connection Failed. Is it a SPP Bluetooth? Try again.");
            finish();
        } else {
            msg("Connected");
            isBtConnected = true;
        }

        progress.dismiss();
    }
}}
public class test extends AppCompatActivity {

String address = null;
private ProgressDialog progress;
BluetoothAdapter myBluetooth = null;
BluetoothSocket btSocket = null;
private boolean isBtConnected = false;
static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");


private TextView connect;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    Intent newint = getIntent();
    address = newint.getStringExtra(DeviceList.EXTRA_ADDRESS);
    setContentView(R.layout.activity_test);




    connect = (TextView) findViewById(R.id.textView4);
    new ConnectBT().execute();


}




private void msg (String s) {
    Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}

private class ConnectBT extends AsyncTask<Void, Void, Void> {
    private boolean ConnectSuccess = true;



    @Override
    protected Void doInBackground (Void... devices) {
        try {
            if ( btSocket==null || !isBtConnected ) {
                myBluetooth = BluetoothAdapter.getDefaultAdapter();
                BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(address);
                btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);
                BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
                btSocket.connect();
            }
        } catch (IOException e) {
            ConnectSuccess = false;
        }

        return null;
    }
    @Override
    protected void onPostExecute (Void result) {
        super.onPostExecute(result);

        if (!ConnectSuccess) {
            msg("Connection Failed. Is it a SPP Bluetooth? Try again.");
            finish();
        } else {
            msg("Connected");
            isBtConnected = true;
        }

        progress.dismiss();
    }

}}
公共类设备列表扩展了AppCompative活动{
按钮btnPaired;
列表视图设备主义者;
专用蓝牙适配器myBluetooth=null;
专用配对设备;
公共静态字符串EXTRA_ADDRESS=“设备地址”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u device\u list);
btnPaired=(按钮)findviewbyd(R.id.Button);
devicelist=(ListView)findViewById(R.id.ListView);
myBluetooth=BluetoothAdapter.getDefaultAdapter();
如果(myBluetooth==null){
Toast.makeText(getApplicationContext(),“蓝牙设备不可用”,Toast.LENGTH_LONG.show();
完成();
}如果(!myBluetooth.isEnabled()),则为else{
Intent-turnBTon=新的Intent(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(turnBTon,1);
}
btnPaired.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
PairedDeviceList();
}
});
}
私有无效PairedDeviceList(){
pairedDevices=myBluetooth.getBondedDevices();
ArrayList=新建ArrayList();
如果(pairedDevices.size()>0){
用于(蓝牙设备bt:pairedDevices){
添加(bt.getName().toString()+“\n”+bt.getAddress().toString());
}
}否则{
Toast.makeText(getApplicationContext(),“未找到配对的蓝牙设备。”,Toast.LENGTH_LONG.show();
}
最终ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,list);
devicelist.setAdapter(适配器);
devicelist.setOnItemClickListener(myListClickListener);
}
私有AdapterView.OnItemClickListener myListClickListener=new AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
字符串信息=((TextView)视图).getText().toString();
字符串地址=信息子字符串(信息长度()-17);
意图i=新意图(DeviceList.this,ledControl.class);
i、 putExtra(额外地址,地址);
星触觉(i);
}
};
LedControl.java

public class DeviceList extends AppCompatActivity {

Button btnPaired;
ListView devicelist;

private BluetoothAdapter myBluetooth = null;
private Set<BluetoothDevice> pairedDevices;
public static String EXTRA_ADDRESS = "device_address";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_device_list);

    btnPaired = (Button) findViewById(R.id.button);
    devicelist = (ListView) findViewById(R.id.listView);

    myBluetooth = BluetoothAdapter.getDefaultAdapter();
    if ( myBluetooth==null ) {
        Toast.makeText(getApplicationContext(), "Bluetooth device not available", Toast.LENGTH_LONG).show();
        finish();
    } else if ( !myBluetooth.isEnabled() ) {
        Intent turnBTon = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(turnBTon, 1);
    }

    btnPaired.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pairedDevicesList();
        }
    });
}

private void pairedDevicesList () {
    pairedDevices = myBluetooth.getBondedDevices();
    ArrayList list = new ArrayList();

    if ( pairedDevices.size() > 0 ) {
        for ( BluetoothDevice bt : pairedDevices ) {
            list.add(bt.getName().toString() + "\n" + bt.getAddress().toString());
        }
    } else {
        Toast.makeText(getApplicationContext(), "No Paired Bluetooth Devices Found.", Toast.LENGTH_LONG).show();
    }

    final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
    devicelist.setAdapter(adapter);
    devicelist.setOnItemClickListener(myListClickListener);
}

private AdapterView.OnItemClickListener myListClickListener = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String info = ((TextView) view).getText().toString();
        String address = info.substring(info.length()-17);

        Intent i = new Intent(DeviceList.this, ledControl.class);
        i.putExtra(EXTRA_ADDRESS, address);
        startActivity(i);
    }
};
public class ledControl extends AppCompatActivity {

Button btn1, btn2, btn3, btn4, btn5, btnDis;
String address = null;
TextView lumn;
private ProgressDialog progress;
BluetoothAdapter myBluetooth = null;
BluetoothSocket btSocket = null;
private boolean isBtConnected = false;
static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent newint = getIntent();
    address = newint.getStringExtra(DeviceList.EXTRA_ADDRESS);

    setContentView(R.layout.activity_led_control);

    btn1 = (Button) findViewById(R.id.button2);
    btn2 = (Button) findViewById(R.id.button3);
    btn3 = (Button) findViewById(R.id.button5);
    btn4 = (Button) findViewById(R.id.button6);
    btn5 = (Button) findViewById(R.id.button7);
    btnDis = (Button) findViewById(R.id.button4);
    lumn = (TextView) findViewById(R.id.textView2);

    new ConnectBT().execute();


    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(ledControl.this, test.class);
            i.putExtra(DeviceList.EXTRA_ADDRESS, address);
            startActivity(i);
        }
    });

}

private void sendSignal ( String number ) {
    if ( btSocket != null ) {
        try {
            btSocket.getOutputStream().write(number.toString().getBytes());
        } catch (IOException e) {
            msg("Error");
        }
    }
}

private void Disconnect () {
    if ( btSocket!=null ) {
        try {
            btSocket.close();
        } catch(IOException e) {
            msg("Error");
        }
    }

    finish();
}

private void msg (String s) {
    Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}

private class ConnectBT extends AsyncTask<Void, Void, Void> {
    private boolean ConnectSuccess = true;

    @Override
    protected  void onPreExecute () {
        progress = ProgressDialog.show(ledControl.this, "Connecting...", "Please Wait!!!");
    }

    @Override
    protected Void doInBackground (Void... devices) {
        try {
            if ( btSocket==null || !isBtConnected ) {
                myBluetooth = BluetoothAdapter.getDefaultAdapter();
                BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(address);
                btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);
                BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
                btSocket.connect();
            }
        } catch (IOException e) {
            ConnectSuccess = false;
        }

        return null;
    }

    @Override
    protected void onPostExecute (Void result) {
        super.onPostExecute(result);

        if (!ConnectSuccess) {
            msg("Connection Failed. Is it a SPP Bluetooth? Try again.");
            finish();
        } else {
            msg("Connected");
            isBtConnected = true;
        }

        progress.dismiss();
    }
}}
public class test extends AppCompatActivity {

String address = null;
private ProgressDialog progress;
BluetoothAdapter myBluetooth = null;
BluetoothSocket btSocket = null;
private boolean isBtConnected = false;
static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");


private TextView connect;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    Intent newint = getIntent();
    address = newint.getStringExtra(DeviceList.EXTRA_ADDRESS);
    setContentView(R.layout.activity_test);




    connect = (TextView) findViewById(R.id.textView4);
    new ConnectBT().execute();


}




private void msg (String s) {
    Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}

private class ConnectBT extends AsyncTask<Void, Void, Void> {
    private boolean ConnectSuccess = true;



    @Override
    protected Void doInBackground (Void... devices) {
        try {
            if ( btSocket==null || !isBtConnected ) {
                myBluetooth = BluetoothAdapter.getDefaultAdapter();
                BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(address);
                btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);
                BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
                btSocket.connect();
            }
        } catch (IOException e) {
            ConnectSuccess = false;
        }

        return null;
    }
    @Override
    protected void onPostExecute (Void result) {
        super.onPostExecute(result);

        if (!ConnectSuccess) {
            msg("Connection Failed. Is it a SPP Bluetooth? Try again.");
            finish();
        } else {
            msg("Connected");
            isBtConnected = true;
        }

        progress.dismiss();
    }

}}
公共类ledControl扩展了AppCompative活动{
按钮btn1、btn2、btn3、btn4、btn5、btnDis;
字符串地址=空;
文本视图列;
私人进展对话进展;
蓝牙适配器myBluetooth=null;
BluetoothSocket btSocket=null;
私有布尔值isBtConnected=false;
静态最终UUID myUUID=UUID.fromString(“00001101-0000-1000-8000-00805F9B34FB”);
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Intent newint=getIntent();
地址=newint.getStringExtra(DeviceList.EXTRA_地址);
setContentView(R.layout.activity\u led\u控件);
btn1=(按钮)findViewById(R.id.button2);
btn2=(按钮)findViewById(R.id.button3);
btn3=(按钮)findViewById(R.id.button5);
btn4=(按钮)findViewById(R.id.button6);
btn5=(按钮)findViewById(R.id.button7);
btnDis=(按钮)findViewById(R.id.button4);
列=(TextView)findViewById(R.id.textView2);
新建ConnectBT().execute();
btn1.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
意图i=新意图(ledControl.this,test.class);
i、 putExtra(DeviceList.EXTRA\u地址,地址);
星触觉(i);
}
});
}
专用无效发送信号(字符串编号){
if(btSocket!=null){
试一试{
btSocket.getOutputStream().write(number.toString().getBytes());
}捕获(IOE异常){
msg(“错误”);
}
}
}
私有无效断开连接(){
if(btSocket!=null){
试一试{
btSocket.close();
}捕获(IOE异常){
msg(“错误”);
}
}
完成();
}
私有void消息(字符串s){
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG.show();
}
私有类ConnectBT扩展异步任务{
private boolean ConnectSuccess=true;
@凌驾
受保护的void onPreExecute(){
progress=ProgressDialog.show(ledControl.this,“正在连接…”,“请稍候!!!”;
}
@凌驾
受保护的Void doInBackground(Void…设备){
试一试{
if(btSocket==null | |!isBtConnected){
myBluetooth=BluetoothAdapter.getDefaultAdapter();
BluetoothDevice dispositivo=myBluetooth.getRemoteDevice(地址);
btSocket=dispositivo.createInsurerCommsocketToServiceRecord(myUUID);
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
btSocket.connect();
}
}捕获(IOE异常){
成功=错误;
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
如果(!连接成功){
msg(“连接失败。是SPP蓝牙吗?请重试”);
完成();
}否则{
msg(“已连接”);
isBtConnected=true;
}
进步。解散();
}
}}
到目前为止,一切都很好。作为一名初学者,我尝试让蓝牙在一个新的活动中工作。但是当我进入这个test.java活动时,我返回到第一个活动“developelist.java”(或崩溃应用程序)

Test.java

public class DeviceList extends AppCompatActivity {

Button btnPaired;
ListView devicelist;

private BluetoothAdapter myBluetooth = null;
private Set<BluetoothDevice> pairedDevices;
public static String EXTRA_ADDRESS = "device_address";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_device_list);

    btnPaired = (Button) findViewById(R.id.button);
    devicelist = (ListView) findViewById(R.id.listView);

    myBluetooth = BluetoothAdapter.getDefaultAdapter();
    if ( myBluetooth==null ) {
        Toast.makeText(getApplicationContext(), "Bluetooth device not available", Toast.LENGTH_LONG).show();
        finish();
    } else if ( !myBluetooth.isEnabled() ) {
        Intent turnBTon = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(turnBTon, 1);
    }

    btnPaired.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pairedDevicesList();
        }
    });
}

private void pairedDevicesList () {
    pairedDevices = myBluetooth.getBondedDevices();
    ArrayList list = new ArrayList();

    if ( pairedDevices.size() > 0 ) {
        for ( BluetoothDevice bt : pairedDevices ) {
            list.add(bt.getName().toString() + "\n" + bt.getAddress().toString());
        }
    } else {
        Toast.makeText(getApplicationContext(), "No Paired Bluetooth Devices Found.", Toast.LENGTH_LONG).show();
    }

    final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
    devicelist.setAdapter(adapter);
    devicelist.setOnItemClickListener(myListClickListener);
}

private AdapterView.OnItemClickListener myListClickListener = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String info = ((TextView) view).getText().toString();
        String address = info.substring(info.length()-17);

        Intent i = new Intent(DeviceList.this, ledControl.class);
        i.putExtra(EXTRA_ADDRESS, address);
        startActivity(i);
    }
};
public class ledControl extends AppCompatActivity {

Button btn1, btn2, btn3, btn4, btn5, btnDis;
String address = null;
TextView lumn;
private ProgressDialog progress;
BluetoothAdapter myBluetooth = null;
BluetoothSocket btSocket = null;
private boolean isBtConnected = false;
static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent newint = getIntent();
    address = newint.getStringExtra(DeviceList.EXTRA_ADDRESS);

    setContentView(R.layout.activity_led_control);

    btn1 = (Button) findViewById(R.id.button2);
    btn2 = (Button) findViewById(R.id.button3);
    btn3 = (Button) findViewById(R.id.button5);
    btn4 = (Button) findViewById(R.id.button6);
    btn5 = (Button) findViewById(R.id.button7);
    btnDis = (Button) findViewById(R.id.button4);
    lumn = (TextView) findViewById(R.id.textView2);

    new ConnectBT().execute();


    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(ledControl.this, test.class);
            i.putExtra(DeviceList.EXTRA_ADDRESS, address);
            startActivity(i);
        }
    });

}

private void sendSignal ( String number ) {
    if ( btSocket != null ) {
        try {
            btSocket.getOutputStream().write(number.toString().getBytes());
        } catch (IOException e) {
            msg("Error");
        }
    }
}

private void Disconnect () {
    if ( btSocket!=null ) {
        try {
            btSocket.close();
        } catch(IOException e) {
            msg("Error");
        }
    }

    finish();
}

private void msg (String s) {
    Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}

private class ConnectBT extends AsyncTask<Void, Void, Void> {
    private boolean ConnectSuccess = true;

    @Override
    protected  void onPreExecute () {
        progress = ProgressDialog.show(ledControl.this, "Connecting...", "Please Wait!!!");
    }

    @Override
    protected Void doInBackground (Void... devices) {
        try {
            if ( btSocket==null || !isBtConnected ) {
                myBluetooth = BluetoothAdapter.getDefaultAdapter();
                BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(address);
                btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);
                BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
                btSocket.connect();
            }
        } catch (IOException e) {
            ConnectSuccess = false;
        }

        return null;
    }

    @Override
    protected void onPostExecute (Void result) {
        super.onPostExecute(result);

        if (!ConnectSuccess) {
            msg("Connection Failed. Is it a SPP Bluetooth? Try again.");
            finish();
        } else {
            msg("Connected");
            isBtConnected = true;
        }

        progress.dismiss();
    }
}}
public class test extends AppCompatActivity {

String address = null;
private ProgressDialog progress;
BluetoothAdapter myBluetooth = null;
BluetoothSocket btSocket = null;
private boolean isBtConnected = false;
static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");


private TextView connect;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    Intent newint = getIntent();
    address = newint.getStringExtra(DeviceList.EXTRA_ADDRESS);
    setContentView(R.layout.activity_test);




    connect = (TextView) findViewById(R.id.textView4);
    new ConnectBT().execute();


}




private void msg (String s) {
    Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}

private class ConnectBT extends AsyncTask<Void, Void, Void> {
    private boolean ConnectSuccess = true;



    @Override
    protected Void doInBackground (Void... devices) {
        try {
            if ( btSocket==null || !isBtConnected ) {
                myBluetooth = BluetoothAdapter.getDefaultAdapter();
                BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(address);
                btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);
                BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
                btSocket.connect();
            }
        } catch (IOException e) {
            ConnectSuccess = false;
        }

        return null;
    }
    @Override
    protected void onPostExecute (Void result) {
        super.onPostExecute(result);

        if (!ConnectSuccess) {
            msg("Connection Failed. Is it a SPP Bluetooth? Try again.");
            finish();
        } else {
            msg("Connected");
            isBtConnected = true;
        }

        progress.dismiss();
    }

}}
公共类测试扩展了AppCompatActivity{
字符串地址=空;
私人进展对话进展;
蓝牙适配器myBluetooth=null;
BluetoothSocket btSocket=null;
私有布尔值isBtConnected=false;
静态最终UUID myUUID=UUID.fromString(“00001101-0000-1000-8000-00805F9B34FB”);
私有文本视图连接