Skip to content

Most visited

Recently visited

navigation
BluetoothAdvertisements / src / com.example.android.bluetoothadvertisements /

MainActivity.java

1
/*
2
 * Copyright (C) 2015 The Android Open Source Project
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
 
17
package com.example.android.bluetoothadvertisements;
18
 
19
import android.bluetooth.BluetoothAdapter;
20
import android.bluetooth.BluetoothManager;
21
import android.content.Context;
22
import android.content.Intent;
23
import android.os.Bundle;
24
import android.support.v4.app.FragmentActivity;
25
import android.support.v4.app.FragmentTransaction;
26
import android.widget.TextView;
27
import android.widget.Toast;
28
 
29
/**
30
 * Setup display fragments and ensure the device supports Bluetooth.
31
 */
32
public class MainActivity extends FragmentActivity {
33
 
34
    private BluetoothAdapter mBluetoothAdapter;
35
 
36
    @Override
37
    protected void onCreate(Bundle savedInstanceState) {
38
        super.onCreate(savedInstanceState);
39
        setContentView(R.layout.activity_main);
40
        setTitle(R.string.activity_main_title);
41
 
42
        if (savedInstanceState == null) {
43
 
44
            mBluetoothAdapter = ((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE))
45
                    .getAdapter();
46
 
47
            // Is Bluetooth supported on this device?
48
            if (mBluetoothAdapter != null) {
49
 
50
                // Is Bluetooth turned on?
51
                if (mBluetoothAdapter.isEnabled()) {
52
 
53
                    // Are Bluetooth Advertisements supported on this device?
54
                    if (mBluetoothAdapter.isMultipleAdvertisementSupported()) {
55
 
56
                        // Everything is supported and enabled, load the fragments.
57
                        setupFragments();
58
 
59
                    } else {
60
 
61
                        // Bluetooth Advertisements are not supported.
62
                        showErrorText(R.string.bt_ads_not_supported);
63
                    }
64
                } else {
65
 
66
                    // Prompt user to turn on Bluetooth (logic continues in onActivityResult()).
67
                    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
68
                    startActivityForResult(enableBtIntent, Constants.REQUEST_ENABLE_BT);
69
                }
70
            } else {
71
 
72
                // Bluetooth is not supported.
73
                showErrorText(R.string.bt_not_supported);
74
            }
75
        }
76
    }
77
 
78
    @Override
79
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
80
        super.onActivityResult(requestCode, resultCode, data);
81
        switch (requestCode) {
82
            case Constants.REQUEST_ENABLE_BT:
83
 
84
                if (resultCode == RESULT_OK) {
85
 
86
                    // Bluetooth is now Enabled, are Bluetooth Advertisements supported on
87
                    // this device?
88
                    if (mBluetoothAdapter.isMultipleAdvertisementSupported()) {
89
 
90
                        // Everything is supported and enabled, load the fragments.
91
                        setupFragments();
92
 
93
                    } else {
94
 
95
                        // Bluetooth Advertisements are not supported.
96
                        showErrorText(R.string.bt_ads_not_supported);
97
                    }
98
                } else {
99
 
100
                    // User declined to enable Bluetooth, exit the app.
101
                    Toast.makeText(this, R.string.bt_not_enabled_leaving,
102
                            Toast.LENGTH_SHORT).show();
103
                    finish();
104
                }
105
 
106
            default:
107
                super.onActivityResult(requestCode, resultCode, data);
108
        }
109
    }
110
 
111
    private void setupFragments() {
112
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
113
 
114
        ScannerFragment scannerFragment = new ScannerFragment();
115
        // Fragments can't access system services directly, so pass it the BluetoothAdapter
116
        scannerFragment.setBluetoothAdapter(mBluetoothAdapter);
117
        transaction.replace(R.id.scanner_fragment_container, scannerFragment);
118
 
119
        AdvertiserFragment advertiserFragment = new AdvertiserFragment();
120
        transaction.replace(R.id.advertiser_fragment_container, advertiserFragment);
121
 
122
        transaction.commit();
123
    }
124
 
125
    private void showErrorText(int messageId) {
126
 
127
        TextView view = (TextView) findViewById(R.id.error_textview);
128
        view.setText(getString(messageId));
129
    }
130
}
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.