eclipse/android/bluetooth

发布于 2021-11-16 22:48:00 字数 5896 浏览 723 评论 3

@JavaGG 你好,想跟你请教个问题:

我用eclipse写android的蓝芽,但一执行就出错


package com.example.bluetooth;


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


import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.widget.Toast;


public class MainActivity extends Activity {
private static BluetoothAdapter mBluetoothAdapter = null; // 用來搜尋、管理藍芽裝置
private static BluetoothSocket mBluetoothSocket = null; // 用來連結藍芽裝置、以及傳送指令
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); // 一定要是這組
private static  OutputStream mOutputStream = null;
private final int REQUEST_ENABLE_BT=1;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            // 如果裝置不支援藍芽
            Toast.makeText(this, "Device doesn't support bluetooth", Toast.LENGTH_SHORT).show();
            return;
        }
              
        // 如果藍芽沒有開啟
        if (!mBluetoothAdapter.isEnabled()) {
            // 發出一個intent去開啟藍芽,
                Intent mIntentOpenBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(mIntentOpenBT, REQUEST_ENABLE_BT);
        }
     // 註冊一個BroadcastReceiver,等等會用來接收搜尋到裝置的消息
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, filter); 
        mBluetoothAdapter.startDiscovery(); //開始搜尋裝置
    }
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive1(Context context, Intent intent) {
            // 當收尋到裝置時
            if (BluetoothDevice.ACTION_FOUND.equals(intent.getAction())) {
                // 取得藍芽裝置這個物件
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                
                // 判斷那個裝置是不是你要連結的裝置,根據藍芽裝置名稱判斷
                if (device.getName().equals("MY_DEVICE_ID")){
                    try {
                        // 一進來一定要停止搜尋
                        mBluetoothAdapter.cancelDiscovery(); 
             
                        // 連結到該裝置
                        mBluetoothSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
                        mBluetoothSocket.connect();
             
                        // 取得outputstream
                        mOutputStream = mBluetoothSocket.getOutputStream();
             
                        // 送出訊息
                        String message = "hello";
                        mOutputStream.write(message.getBytes());
             
                        } catch (IOException e) {
      
                        }
                }
           
            }
       }


@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

}
};
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心欲静而疯不止 2021-11-18 17:04:53

需要補充什麼嗎?

秉烛思 2021-11-18 03:48:50

  你得把错误信息贴上来

毁梦 2021-11-16 23:53:57

  你得把错误信息贴上来

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文