Android的蓝牙SPP银河S3蓝牙、银河、Android、SPP

2023-09-07 02:02:15 作者:难过泛滥成河

我试图建立一个三星Galaxy S3之间的接驳蓝牙与Android 4.0.3和RN 42蓝牙芯片上运行,的此处是模型:

I'm trying to establish a bluetooth connexion between a Samsung Galaxy S3 running with Android 4.0.3 and a RN 42 Bluetooth Chip, here is the model :

我用过很多的不同的codeS的设立蓝牙串行通信,和所有的人都表现得十分出色的HTC野火我之前使用。但现在,我使用的是银河S3手机,这是不可能建立了连接。这里是code我使用,我发现它的这里:

I used lot's of different codes for setting up a bluetooth serial communication, and all of them are working very well on the HTC Wildfire i was using before. But now that i'm using the galaxy s3 phone, it's impossible to establish the connexion. Here is the code i'm using, i found it here :

我把Android清单的权限。

I put the permissions in the android manifest.

首先,我的蓝牙接口:

public class BtInterface {

private BluetoothDevice device = null;
private BluetoothSocket socket = null;
private InputStream receiveStream = null;
private OutputStream sendStream = null;

private ReceiverThread receiverThread;

Handler handler;

public BtInterface(Handler hstatus, Handler h) {
    Set<BluetoothDevice> setpairedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
    BluetoothDevice[] pairedDevices = (BluetoothDevice[]) setpairedDevices.toArray(new BluetoothDevice[setpairedDevices.size()]);

    for(int i=0;i<pairedDevices.length;i++) {
        if(pairedDevices[i].getName().contains("MyBluetoothChip")) {
            device = pairedDevices[i];
            try {
                socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
                receiveStream = socket.getInputStream();
                sendStream = socket.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
        }
    }

    handler = hstatus;

    receiverThread = new ReceiverThread(h);
}

public void sendData(String data) {
    sendData(data, false);
}

public void sendData(String data, boolean deleteScheduledData) {
    try {
        sendStream.write(data.getBytes());
        sendStream.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void connect() {
    new Thread() {
        @Override public void run() {
            try {
                socket.connect();

                Message msg = handler.obtainMessage();
                msg.arg1 = 1;
                handler.sendMessage(msg);

                receiverThread.start();

            } catch (IOException e) {
                Log.v("N", "Connection Failed : "+e.getMessage());
                e.printStackTrace();
            }
        }
    }.start();
}

public void close() {
    try {
        socket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public BluetoothDevice getDevice() {
    return device;
}

private class ReceiverThread extends Thread {
    Handler handler;

    ReceiverThread(Handler h) {
        handler = h;
    }

    @Override public void run() {
        while(true) {
            try {
                if(receiveStream.available() > 0) {

                    byte buffer[] = new byte[100];
                    int k = receiveStream.read(buffer, 0, 100);

                    if(k > 0) {
                        byte rawdata[] = new byte[k];
                        for(int i=0;i<k;i++)
                            rawdata[i] = buffer[i];

                        String data = new String(rawdata);

                        Message msg = handler.obtainMessage();
                        Bundle b = new Bundle();
                        b.putString("receivedData", data);
                        msg.setData(b);
                        handler.sendMessage(msg);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
}

然后,我的主要活动,只需一键连接和聊天:

Then, my main activity, with just a button to connect and a chat :

public class MonApp extends Activity implements OnClickListener {

private TextView logview;
private EditText sendtext;
private Button connect, send;

private BtInterface bt = null;

private long lastTime = 0;

final Handler handler = new Handler() {
    public void handleMessage(Message msg) {
        String data = msg.getData().getString("receivedData");

        long t = System.currentTimeMillis();
        if(t-lastTime > 100) {// Pour Èviter que les messages soit coupÈs
            logview.append("\n");
            lastTime = System.currentTimeMillis();
        }
        logview.append(data);
    }
};

final Handler handlerStatus = new Handler() {
    public void handleMessage(Message msg) {
        int co = msg.arg1;
        if(co == 1) {
            logview.append("Connected\n");
        } else if(co == 2) {
            logview.append("Disconnected\n");
        }
    }
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    bt = new BtInterface(handlerStatus, handler);

    logview = (TextView)findViewById(R.id.logview);
    sendtext = (EditText)findViewById(R.id.sendtxt);

    connect = (Button)findViewById(R.id.connect);
    connect.setOnClickListener(this);

    send = (Button)findViewById(R.id.send);
    send.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    if(v == connect) {
        bt.connect();
    } else if(v == send) {
        bt.sendData(sendtext.getText().toString());
    }
}
}

当我尝试连接到芯片,这是我从logcat中获得:

When i try to connect to the chip, this is what i get from the logcat :

09-05 11:37:05.515: I/BluetoothPolicyService(2097): getBluetoothDataTransferAllowed 
09-05 11:37:05.520: D/BluetoothPolicyService(2097): MDM: isProfileEnabled = true
09-05 11:37:05.520: D/BluetoothUtils(6868): isSocketAllowedBySecurityPolicy start : device null
09-05 11:37:05.525: V/BluetoothService.cpp(2097): createDeviceNative
09-05 11:37:05.525: V/BluetoothService.cpp(2097): createDeviceNative
09-05 11:37:05.525: V/BluetoothEventLoop.cpp(2097): onCreateDeviceResult
09-05 11:37:05.525: V/BluetoothEventLoop.cpp(2097): onCreateDeviceResult
09-05 11:37:05.525: E/BluetoothEventLoop.cpp(2097): onCreateDeviceResult: D-Bus error: org.bluez.Error.AlreadyExists (Already Exists)
09-05 11:37:05.525: V/BluetoothService.cpp(2097): discoverServicesNative
09-05 11:37:05.525: V/BluetoothService.cpp(2097): ... Object Path = /org/bluez/3094/hci0/dev_00_06_66_43_A1_E6
09-05 11:37:05.525: V/BluetoothService.cpp(2097): ... Pattern = , strlen = 0
09-05 11:37:10.660: V/BluetoothEventLoop.cpp(2097): onDiscoverServicesResult
09-05 11:37:10.660: V/BluetoothEventLoop.cpp(2097): ... Device Path = /org/bluez/3094/hci0/dev_00_06_66_43_A1_E6
09-05 11:37:10.660: E/BluetoothEventLoop.cpp(2097): onDiscoverServicesResult: D-Bus error: org.bluez.Error.ConnectionAttemptFailed (Host is down)
09-05 11:37:10.670: V/N(6868): Connection Failed : Service discovery failed
09-05 11:37:10.670: W/System.err(6868): java.io.IOException: Service discovery failed
09-05 11:37:10.675: W/System.err(6868):     at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:462)
09-05 11:37:10.675: W/System.err(6868):     at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:240)
09-05 11:37:10.675: W/System.err(6868):     at com.example.bluetooth.BtInterface$1.run(BtInterface.java:68)

我也没有想通为什么code没有与银河S3的工作。我试图搜索谷歌关于蓝牙兼容性的问题,但我没有发现任何东西为止。

I cannot figured out why this code doesn't work with the galaxy s3. I tried to search on google about bluetooth compatibility issues but i didn't find anything so far.

感谢您!

纪尧姆

推荐答案

有大量的蓝牙兼容性问题与新的Andr​​oid ICS。显然,他们搞砸与周围的蓝牙code为提高安全性。

There are plenty of Bluetooth incompatibility issues with the new Android ICS. Apparently they messed around with the Bluetooth code to 'improve security'.

A 错误我发现并报告(这让我的整个嵌入式主板不能够一个星期有任何的Andr​​oid ICS的设备连接),是建立一个已配对的设备连接时,安卓ICS会需要重新验证 - 如果你使用蓝牙SSP (我用它带或不带PIN依赖 - 这意味着,对我来说,在2保税设备Android ICS连接仍然是可能的,而无需用户交互 - 但它需要多一点的时间)

A bug I found and reported(which made my entire embedded board not able to connect for a week with any Android ICS device) was that when establishing a connection to an already paired device, Android ICS would require re-authentication - with or without PIN depends if you use Bluetooth SSP (I use it - this means that for me, on Android ICS connection of 2 bonded devices is still possible without user interaction - but it takes a bit longer).

请投票谷歌的错误追踪这个bug - 也许有人会解决它...

这也许是你的问题呢?你能在哪一行的code减免究竟指定? 环绕所有的蓝牙系统code用的try / catch等。

Maybe this is your problem as well? Can you specify exactly at what line your code breaks? Surround all your Bluetooth system code with try/catch, etc.

总之,新的蓝牙行为是非常非常糟糕,我听到很多人不能够连接,因为它给他们的旧耳机。

Anyway, the new Bluetooth behavior is very very bad, I heard of a lot of people not being able to connect to their old headset because of it.