Поиск блютуз устройств.

Ответить
masta
Сообщения: 12
Зарегистрирован: 05 авг 2014, 16:34

Поиск блютуз устройств.

Сообщение masta » 23 июл 2016, 16:47

Добрый день, хотелось реализовать поиск блютуз устройств.

Код: Выделить всё

package domain.my.bl_recieve_data_slave;

import java.io.OutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.util.UUID;
import java.lang.reflect.Method;

import android.os.Handler;
import android.util.Log;
import android.content.Intent;

import android.view.View;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ListView;
import android.widget.ArrayAdapter;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;

import java.util.ArrayList;

public class MainActivity extends Activity {

    public TextView mytext;

    Handler h;
    final int ArduinoData = 1;
    ConnectedThred MyThred = null;
    static String MacAddress = "98:D3:31:90:0C:A2"; // MAC-адрес БТ модуля
   // private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    private  BluetoothSocket clientSocket = null;
    private ArrayList<String> mDeviceList = new ArrayList<String>();
    private ListView listView;
    private BluetoothAdapter mBluetoothAdapter;

    //этот метод вызывается при создании или перезапуске приложения
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mBluetoothAdapter.startDiscovery();

        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, filter);

         h = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case ArduinoData:
                    byte[] readBuf = (byte[]) msg.obj;
                    String strIncom = new String(readBuf, 0, msg.arg1);
                    mytext.setText("Данные от Arduino: " + strIncom);
                    break;
            }
        };
    };

    MyThred = new ConnectedThred(clientSocket);
    MyThred.start();
}


    @Override
    protected void onDestroy() {
        unregisterReceiver(mReceiver);
        super.onDestroy();
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                BluetoothDevice device = intent
                        .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                mDeviceList.add(device.getName() + "\n" + device.getAddress());
                Log.i("BT", device.getName() + "\n" + device.getAddress());
                listView.setAdapter(new ArrayAdapter<String>(context,
                        android.R.layout.simple_list_item_1, mDeviceList));
            }
        }
    };

}



main_activity.xml

Код: Выделить всё

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"/>

</RelativeLayout>
manifest.xml

Код: Выделить всё

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="domain.my.bl_recieve_data_slave" >

    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Программа успешно компилируется, но списка не видно, как тут.
http://prntscr.com/bwiom6

Ответить