خرید بک لینک

Vote count: 0

I don't know what's wrong with it, but my app is searching for nearby Bluetooth devices, but the moment I'm trying to coect to one of these Bluetooth devices, the app crashes.

Please have a look

This is my SearchBTDevice.java. This is the activity that calls the CoectBTDevice activity

package vertex2016.mvjce.edu.bluealert;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGattDescriptor;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.bluetooth.BluetoothAdapter;
import android.provider.Settings;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.Set;

public class SearchBTDevice extends AppCompatActivity {

    public BluetoothAdapter BlueAdapter = BluetoothAdapter.getDefaultAdapter();
    public ArrayAdapter PairedArrayAdapter;
    public ArrayAdapter BTArrayAdapter;
    BluetoothDevice btd;

    public ListView devicesFound;


    private final BroadcastReceiver BTReceiver= new BroadcastReceiver(){

       public void onReceive(Context context, Intent intent)
       {
           String action = intent.getAction();

           if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                   btd = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                   BTArrayAdapter.add(btd.getName() + "t" + btd.getAddress() + "n");

               }
           }

    };

    IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_FOUND);




    @Override
    protected void onResume() {
        super.onResume();
        this.registerReceiver(BTReceiver,filter1);



    }

    @Override
    protected void onPause() {
        super.onPause();
        BlueAdapter.cancelDiscovery();
        this.unregisterReceiver(BTReceiver);
        Toast.makeText(SearchBTDevice.this, "Discovery Stopped!!", Toast.LENGTH_SHORT).show();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_btdevice);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);




       searchBTDevices();


    }


    public void searchBTDevices()
    {
        if(!BlueAdapter.startDiscovery())
            Toast.makeText(SearchBTDevice.this, "Failed to Start Discovery", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(SearchBTDevice.this, "Discovery Startred", Toast.LENGTH_SHORT).show();
        BTArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1);
        devicesFound = (ListView)findViewById(R.id.searchpagelistView);
        devicesFound.setAdapter(BTArrayAdapter);
        devicesFound.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent coectedBTintent = new Intent(SearchBTDevice.this, CoectedBTDevice.class);
                coectedBTintent.putExtra("BluetoothDevice", btd);
                startActivity(coectedBTintent);

            }
        });

    }


}

This is my CoectBTDevice.java that attempts to make establish the RFCOMM chael. But this is where my app crashes.

package vertex2016.mvjce.edu.bluealert;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

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

public class CoectedBTDevice extends AppCompatActivity {

    public BluetoothDevice btd;
    public BluetoothSocket btSocket, tempSocket;
    private UUID myUUID;
    ArrayAdapter arr = new ArrayAdapter(this, android.R.layout.simple_list_item_2);
    ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coected_btdevice);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);

       btd = getIntent().getParcelableExtra("BluetoothDevice");

        coectBT();
        displayStuff();

    }

    public void coectBT()
    {
        Thread myThread = new Thread() {

            public void run()
            {
                tempSocket=null;

                try {
                    tempSocket  = btd.createRfcommSocketToServiceRecord(myUUID);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                BluetoothAdapter.getDefaultAdapter().cancelDiscovery();

                try {
                    btSocket.coect();
                    arr.add("CONNECTED TO-->"+btd.getName());
                } catch (IOException e) {
                    e.printStackTrace();
                    try {
                        btSocket.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }

            }

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

        myThread.start();
    }

    void displayStuff()
    {
        lv = (ListView)findViewById(R.id.coectedBTlistView);
        lv.setAdapter(arr);
    }

}

This is what my logcat shows

03-19 10:56:25.072 6086-6086/vertex2016.mvjce.edu.bluealert E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: vertex2016.mvjce.edu.bluealert, PID: 6086
                                                                              java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{vertex2016.mvjce.edu.bluealert/vertex2016.mvjce.edu.bluealert.CoectedBTDevice}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
                                                                                  at android.app.ActivityThread.access$900(ActivityThread.java:154)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:135)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5292)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                  at com.android.inteal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                                                  at com.android.inteal.os.ZygoteInit.main(ZygoteInit.java:699)
                                                                               Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
                                                                                  at android.app.Activity.getSystemService(Activity.java:5035)
                                                                                  at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
                                                                                  at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:104)
                                                                                  at vertex2016.mvjce.edu.bluealert.CoectedBTDevice.<init>(CoectedBTDevice.java:24)
                                                                                  at java.lang.reflect.Constructor.newInstance(Native Method)
                                                                                  at java.lang.Class.newInstance(Class.java:1606)
                                                                                  at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2259)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) 
                                                                                  at android.app.ActivityThread.access$900(ActivityThread.java:154) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                  at android.os.Looper.loop(Looper.java:135) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5292) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                  at com.android.inteal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
                                                                                  at com.android.inteal.os.ZygoteInit.main(ZygoteInit.java:699)

asked 49 secs ago

برچسب: نویسنده: استخدام کار تاريخ: شنبه 29 اسفند 1394 ساعت: 11:40

صفحه بندی