mercoledì 9 aprile 2014

Proximity Sensor con Bluetooth 2 in Android (2)

Questo e' il seguito del precedente post
Qui e' stato inserito un timer che ogni 10 secondi effettua un discovery dei dispositivi bluetooth vicini e se il dispositivo ha un certo nome e se il suo segnale' sufficientemente forte mostra una determinata pagina html su una WebView

Link al codice sorgente


MainActivity.java (evidenziate in giallo le linee di codice piu' interessanti)
----------------------------------------------------
package com.luca.innocenti.btproximity;

import java.util.Timer;
import java.util.TimerTask;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;


@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends ActionBarActivity {
    private static BluetoothAdapter BTAdapter = BluetoothAdapter.getDefaultAdapter();

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

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}

        registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));


Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
     public void run() {
     runOnUiThread(new Runnable() {
      @Override
      public void run() {
      BTAdapter.startDiscovery();
     
      }
    
      });
      }
      }, 0, 10000);
    }


private final BroadcastReceiver receiver = new BroadcastReceiver(){
       
 @Override
 public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
            if(BluetoothDevice.ACTION_FOUND.equals(action)) {
                int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
                String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
                if (name.equals("GT") && (rssi >= -50))
                {
                html5.loadUrl("file:///android_asset/www/1.html");
                }
                
                if (name.equals("GT2") && (rssi >= -50))
                {
                html5.loadUrl("file:///android_asset/www/2.html");
                }

            }
           
  
  
 }
   };
public static WebView html5;

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);

html5 = (WebView) rootView.findViewById(R.id.webView1);
html5.getSettings().setJavaScriptEnabled(true);
html5.setWebChromeClient(new WebChromeClient());
html5.loadUrl("file:///android_asset/www/index.html");
return rootView;
}
}

}
----------------------------------------------------

Manifest
----------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.luca.innocenti.btproximity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    
    <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="com.luca.innocenti.btproximity.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>
----------------------------------------------------

fragment_main.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.luca.innocenti.btproximity.MainActivity$PlaceholderFragment" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>
----------------------------------------------------

Nessun commento:

Posta un commento

Riparazione volante Logitech G920

Con il tempo la pedaliera del Logitech G920 ha cominciato a fare i capricci in particolare per quanto riguarda l'acceleratore Ho provato...