English 中文(简体)
1. 甲型六氯环己烷
原标题:Start bluetooth in android service

我正在开发一项申请,该申请应当从蓝色扫描机开入一个 and器服务处(不要求用户使蓝色宇宙涂料完全没有活动),而我的器具没有或更确切地说,我的装置没有LCD/LED显示。 在全面搜查和搜查之后,我可以部分地找到解决办法,并写下以下法典。

在这里,我开始从广播接收人那里获得服务,在我服务时,我开始穿蓝色,但蓝色衣似乎在转头,我尝试使蓝色人能够直接使用该守则。

BluetoothAdapter.getDefaultAdapter().enable()

审判

Intent btIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
btIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
serviceContext.startActivity(btIntent);

但是,在这两种情况下,蓝色体不会改变。

此处为:

android:versionCode="1"
android:versionName="1.0" android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <receiver android:name=".StartReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>

    <service android:name=".StartService"></service>
</application>

这里是广播接收器:

public class StartReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    Intent receiverIntent = new Intent(context, StartService.class);
    context.startService(receiverIntent);
    Log.i("Autostart", "started");
}
}

服务班:

public class StartService extends Service{

private static final String TAG = "BluetoothService";
BluetoothAdapter btAdapter;
BluetoothDevice device;

Context serviceContext;

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public void onStart(Intent intent, int startid){
    Toast.makeText(this, "Bluetooth Service Started", Toast.LENGTH_LONG).show();
    Log.d(TAG, "OnStart");

    BluetoothAdapter.getDefaultAdapter().enable();
    //Intent btIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    //btIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    //serviceContext.startActivity(btIntent);

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

        btAdapter.startDiscovery();

 }

@Override
public void onDestroy(){
    BluetoothAdapter.getDefaultAdapter().disable();
    unregisterReceiver(bcReceiver);
    btAdapter.cancelDiscovery();
}

private final BroadcastReceiver bcReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        if(BluetoothDevice.ACTION_FOUND.equals(action)){

            //Do Something
        }
    }
};
}

另外,服务班中使用的Toast电文没有显示。

我的法典中什么可能错了,因为不扶持和安放;扫描蓝to。

问题回答

页: 1

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "Bluetooth Service Started", Toast.LENGTH_LONG).show();

        Log.e("in onStartCommand", "onStartCommand");
        BluetoothAdapter.getDefaultAdapter().enable();
       //Intent btIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
       //btIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       //serviceContext.startActivity(btIntent);

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

       btAdapter.startDiscovery();

       return super.onStartCommand(intent, flags, startId);
    }




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签