Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 蓝牙不写也不花很长时间阅读 导入android.annotation.TargetApi; 导入android.bluetooth.BluetoothAdapter; 导入android.bluetooth.bluetooth设备; 导入android.b_Java_Android_Bluetooth - Fatal编程技术网

Java 蓝牙不写也不花很长时间阅读 导入android.annotation.TargetApi; 导入android.bluetooth.BluetoothAdapter; 导入android.bluetooth.bluetooth设备; 导入android.b

Java 蓝牙不写也不花很长时间阅读 导入android.annotation.TargetApi; 导入android.bluetooth.BluetoothAdapter; 导入android.bluetooth.bluetooth设备; 导入android.b,java,android,bluetooth,Java,Android,Bluetooth,蓝牙不写也不花很长时间阅读 导入android.annotation.TargetApi; 导入android.bluetooth.BluetoothAdapter; 导入android.bluetooth.bluetooth设备; 导入android.bluetooth.BluetoothServerSocket; 导入android.bluetooth.BluetoothSocket; 导入android.content.Intent; 导入android.content.res.Confi

蓝牙不写也不花很长时间阅读
导入android.annotation.TargetApi;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.bluetooth设备;
导入android.bluetooth.BluetoothServerSocket;
导入android.bluetooth.BluetoothSocket;
导入android.content.Intent;
导入android.content.res.Configuration;
导入android.os.Bundle;
导入android.os.Handler;
导入android.os.Message;
导入android.support.v7.app.AppActivity;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入android.widget.Toast;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.OutputStream;
导入java.util.Set;
导入java.util.UUID;
@塔吉塔皮(18)
公共类MainActivity扩展了AppCompatActivity{
`int b=0;
BluetoothAdapter BA=BluetoothAdapter.getDefaultAdapter();
蓝牙设备;
蓝牙;
BluetoothServerSocket服务器;
UUID UUID=UUID.fromString(“f63b93f5-56e0-47bb-8972-996e34cfb9a8”)`
`@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main)`
`意向i=新意向(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
星触觉(i)`
`接受=新接受();
accept.start()`
Set devices=BA.getBondedDevices();
用于(蓝牙设备bt:devices){
试一试{
if(bt.getName().equals(“霸王”)){
device=BA.getRemoteDevice(bt.getAddress());
BS=设备.createrFComSocketToServiceRecord(uuid);
}else if(bt.getName().equals(“华为Y5”)){
device=BA.getRemoteDevice(bt.getAddress());
BS=设备.createrFComSocketToServiceRecord(uuid);
}
}捕获(IOE异常IOE){}
}
}
公共接口消息内容{
int MSG_MESSAGE=0;
}
Handler=newhandler(){
公共无效handleMessage(消息消息消息){
TextView tx=(TextView)findViewById(R.id.textView2);
开关(msg.what){
case MessageContents.MSG_消息:
字节[]readBuf=(字节[])msg.obj;
//从缓冲区中的有效字节构造字符串
String readMessage=新字符串(readBuf,0,msg.arg1);
Log.d(“标签”,readMessage);
tx.setText(readMessage);
打破
违约:
tx.setText(“无文本”);
}
}
};
类连接扩展线程{
公开募捐{
试一试{
试一试{
连接();
Log.d(“Log”、“e”);
Log.d(“Log”,“r”+BS.isConnected());
DataTransfer DataTransfer=新数据传输();
dataTransfer.start();
}捕获(NullPointerException e){
Log.d(“Log”、“meezaan”);
}
}捕获(IOE异常){
Log.d(“Log”、“t”);
}
}
}
类接受扩展线程{
公开募捐{
试一试{
while(true){
server=BA.listenusingrfcommwithservicecerecord(“安全连接”,uuid);
accept();
日志d(“日志”、“接受”);
}
}捕获(IOE异常){
}
}
}
类DataTransfer扩展了线程{
@凌驾
公开募捐{
int d=1;
InputStream接收=null;
Log.d(“登录”、“登录”);
整数单位;
字节[]缓冲区=新字节[1024];
试一试{
receive=BS.getInputStream();
Log.d(“Log”、“pn”);
while(BS.isConnected()){
试一试{

谢谢你的编辑。你对我的问题有什么建议或解决办法吗
import android.annotation.TargetApi;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;

@TargetApi(18)
public class MainActivity extends AppCompatActivity{

`int b = 0;
BluetoothAdapter BA = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device;
BluetoothSocket BS;
BluetoothServerSocket server;
UUID uuid = UUID.fromString("f63b93f5-56e0-47bb-8972-996e34cfb9a8");`

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

    `Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivity(i);`

    `Accept accept = new Accept();
    accept.start();`

    Set<BluetoothDevice> devices = BA.getBondedDevices();
    for(BluetoothDevice bt : devices) {
        try {
            if (bt.getName().equals("Overlord")) {
                device = BA.getRemoteDevice(bt.getAddress());
                BS = device.createRfcommSocketToServiceRecord(uuid);
            } else if (bt.getName().equals("Huawei Y5")) {
                device = BA.getRemoteDevice(bt.getAddress());
                BS = device.createRfcommSocketToServiceRecord(uuid);
            }
        }catch(IOException IOE){}
    }
}

public interface MessageContents{
    int MSG_MESSAGE = 0;
}
Handler handler = new Handler(){
    public void handleMessage(Message msg) {
        TextView tx = (TextView)findViewById(R.id.textView2);
        switch(msg.what){
            case MessageContents.MSG_MESSAGE:
                byte[] readBuf = (byte[]) msg.obj;
                // construct a string from the valid bytes in the buffer
                String readMessage = new String(readBuf, 0, msg.arg1);
                Log.d("TAG", readMessage);
                tx.setText(readMessage);
                break;

            default:
                tx.setText("No text");
        }
    }
};



class Connect extends Thread{
    public void run(){
        try {
            try {
                    BS.connect();
                    Log.d("LOG", "  e");
                    Log.d("LOG", "r" + BS.isConnected());

                DataTransfer dataTransfer = new DataTransfer();
                dataTransfer.start();
                }catch(NullPointerException e){
                Log.d("LOG", "  meezaan");
            }
        }catch(IOException e){
            Log.d("LOG", "t");
        }
    }

}

class Accept extends Thread{

    public void run() {
        try {
            while (true) {
                server = BA.listenUsingRfcommWithServiceRecord("Secure Connection", uuid);
                server.accept();
                Log.d("LOG", "accept");
            }
        }catch(IOException E) {
        }
    }
}

class DataTransfer extends Thread{

    @Override
    public void run() {
        int d = 1;
        InputStream receive = null;
        Log.d("LOG", "On");
        int numBytes;
        byte[] buffer = new byte[1024];
        try {
            receive = BS.getInputStream();
            Log.d("LOG", "pn");
            while (BS.isConnected()) {
                try {
                    if (d <= 5) {
                        Log.d("LOG", "did");
                        ++d;
                    }
                    numBytes = receive.read(buffer);
                    handler.obtainMessage(MessageContents.MSG_MESSAGE, numBytes, -1, buffer).sendToTarget();
                } catch (NullPointerException e) {
                    Log.d("LOG", "error read(null)");
                }
            }
            Log.d("LOG", "pnA");
        } catch (IOException IOE) {
            Log.d("LOG", "error read(IOE)");
        }

    }
}

public void onOne(View view){
    write("1".getBytes());
}

public void onClick(View view){
    Button b1 = (Button) findViewById(R.id.button);
    EditText pin = (EditText) findViewById(R.id.editText);
    pin.setRawInputType(Configuration.KEYBOARD_QWERTY);
    pin.setVisibility(View.VISIBLE);
    b1.setVisibility(View.VISIBLE);

}

public void pin (View view) {
    Button b1 = (Button)findViewById(R.id.button);
    EditText pin = (EditText)findViewById(R.id.editText);
    String pin1 = pin.getText().toString();
    if (pin1.equals("")) {
        Connect connect = new Connect();
        connect.run();
        pin.setVisibility(View.INVISIBLE);
        b1.setVisibility(View.INVISIBLE);
    } else {
        Toast.makeText(getApplicationContext(), "Wrong Pin!"+pin1, Toast.LENGTH_SHORT).show();
        pin.setVisibility(View.INVISIBLE);
        b1.setVisibility(View.INVISIBLE);
    }
}

public void onTwo(View v){
    Toast.makeText(getApplicationContext(), "" + handler.hasMessages(MessageContents.MSG_MESSAGE) + " " + BS.isConnected(), Toast.LENGTH_SHORT).show();
}

public void write(byte[] Byte){
    try {
        OutputStream transfer = BS.getOutputStream();
        try {
            transfer.write(Byte);
        }catch(NullPointerException e){
            Log.d("LOG", "error write(null) " + BS.isConnected());}
    }catch (IOException IOE){ Log.d("LOG", "b");
        Log.d("LOG", "error write(IOE) " + BS.isConnected());}
}

}`