无法通过蓝牙连接

jike_yoummiegao Android基础知识-传感器... 最后由 arjinmc 于2015年12月29日回复

  • 1 回答
  • 1.9k 浏览

按照视频中的代码,运行程序,发现无法连接其他Android设备或者电脑设备

Connect_Fail.png

代码如下:

package com.example.yoummiegao.bluetoothsocket;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
//import java.util.logging.Handler;
import java.util.logging.LogRecord;

public class MainActivity extends Activity implements AdapterView.OnItemClickListener{

    private ListView lvDevices;
    private BluetoothAdapter bluetoothAdapter;

    private List<String> bluetoothDevices = new ArrayList<String>();

    private ArrayAdapter<String> arrayAdapter;

    //private  final UUID MY_UUID = UUID.fromString("fe09b933-ba57-42b7-a50f-cca4c7cda13e");
    private  final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    private final  String NAME = "Bluetooth_Socket";

    private BluetoothSocket clientSocket;
    private BluetoothDevice device;
    private AcceptThread acceptThread;
    private OutputStream os;

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

        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        lvDevices = (ListView) findViewById(R.id.lvDevices);

        Set<BluetoothDevice> piredDevices = bluetoothAdapter.getBondedDevices();

        if(piredDevices.size()>0)
        {
            for(BluetoothDevice device:piredDevices)
            {
                bluetoothDevices.add(device.getName()+":"+device.getAddress()+"n");
            }
        }

        arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,bluetoothDevices);

        lvDevices.setAdapter(arrayAdapter);
        lvDevices.setOnItemClickListener(this);
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        this.registerReceiver(receiver, filter);

        filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        this.registerReceiver(receiver, filter);

        acceptThread = new AcceptThread();
        acceptThread.start();

    }

    public void onClick_Search(View view) {
        setProgressBarIndeterminateVisibility(true);
        setTitle("正在扫描...");

        if (bluetoothAdapter.isDiscovering()) {
            bluetoothAdapter.cancelDiscovery();
        }
        bluetoothAdapter.startDiscovery();
    }

    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (BluetoothDevice.ACTION_FOUND.equals(action)) {

                BluetoothDevice device = intent
                        .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                    bluetoothDevices.add(device.getName() + ":"
                            + device.getAddress() + "n");
                    arrayAdapter.notifyDataSetChanged();
                }

            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
                    .equals(action)) {
                setProgressBarIndeterminateVisibility(false);
                setTitle("连接蓝牙设备");

            }
        }
    };


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        String s = arrayAdapter.getItem(position);
        String address = s.substring(s.indexOf(":")+1).trim();

        try
        {
            if(bluetoothAdapter.isDiscovering())
            {
                bluetoothAdapter.cancelDiscovery();
            }
            try
            {
                if(device == null)
                {
                    device = bluetoothAdapter.getRemoteDevice(address);
                }
                if(clientSocket == null)
                {
                    clientSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
                    clientSocket.connect();

                    os = clientSocket.getOutputStream();
                }
       					
  • arjinmc 2015年12月29日 回答 #1楼
  • 你声明了权限,注册了广播接受者没?

  • 0 评论