Frage

I'm brand new to Android and I'm trying to test the new Bluetooth LE functionality Android 4.3 has. I've followed the documentation found here on the Android site. But i'm at a stumbling block of actually starting my code.

I was thinking of simply having a button that called my scanLeDevice code, but I can't see anything that can allow me to do this simply. Literally all I have is this class and the sample hello word app they make when you make a new project.

Can anyone help me with this? I know it's noobish, but I'm really stumped.

For reference, my Bluetooth class is this:

public class DeviceScanActivity extends ListActivity {

private BluetoothAdapter mBluetoothAdapter;
private boolean mScanning;
private Handler mHandler;

// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 10000;

private ArrayAdapter<Object> list;
private Handler handler = new Handler();

protected void OnCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    list = new ArrayAdapter<Object>(getApplicationContext(), android.R.layout.simple_list_item_1);
    setListAdapter(list);
    scanLeDevice(true);
}

@TargetApi(18)
public void scanLeDevice(final boolean enable) {
    list.add("Scanning...");
    final BluetoothAdapter adapter = getBluetoothAdapter();
    if (enable) {
        // Stops scanning after a pre-defined scan period.
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mScanning = false;

               // mBluetoothAdapter.stopLeScan(mLeScanCallback);
                adapter.stopLeScan(callback);
                list.clear();
            }
        }, SCAN_PERIOD);

        mScanning = true;
        adapter.startLeScan(callback);
    } else {
        mScanning = false;
        adapter.stopLeScan(callback);
    }

}
@TargetApi(18)
private BluetoothAdapter getBluetoothAdapter()
{
    BluetoothManager manager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    return manager.getAdapter();
}
private final BluetoothAdapter.LeScanCallback callback = new BluetoothAdapter.LeScanCallback() 
{

    @Override
    public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
        // TODO Auto-generated method stub

        {
            list.add("found: " + device);
            runOnUiThread(new Runnable(){
                @Override
                public void run()
                {
                    list.add(device);
                    list.notifyDataSetChanged();
                }
            });
    }
}
};

}

War es hilfreich?

Lösung

Add a new Button to your MainActivity:

<Button
    android:id="@+id/btnCallMethod"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="Call Method" />

Put this in the onCreate() of MainActivity

addListeners();

That's how the addListeners() method should look like in the MainActivity:

private void addListeners() {
  Button btnCallMethod = (Button) this.findViewById(R.id.btnCallMethod);

  // CALL METHOD BUTTON
  btnCallMethod.setOnTouchListener(new OnTouchListener() {

     @Override
     public boolean onTouch(View v, MotionEvent event) {
     if (event.getAction() == MotionEvent.ACTION_UP) {
       Intent intent = new Intent(getApplicationContext(), DeviceScanActivity.class);
       startActivity(intent); // start the DeciveScanActivity
     }
     return false;
   }
 });
}

Now call the method in the onCreate() method of the DeviceScanActivity

Don't forget to add your DeviceScanActivity to your AndroidManifest.xml

<activity
     android:name="com.example.example.DeviceScanActivity"
</activity>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top