質問内容
Androidアプリを開発しており、WiFi Direct接続をしている時の電波強度を取得したいと思っています。
Googleが公開している Git - WiFiP2pPeer にはおそらく取得できそうなコードが載っているのですが、使用方法がわかりません。
下記コードをAndroid Studioなどで自分のアプリのコードにどう反映すれば電波強度を取得できるでしょうか?
また、別の方法で電波強度が取得できる方法などがあれば教えてください。
--やってみたこと--
jarファイルかなにかをlibsフォルダに配置するのかと思ったのですが、
gitからダウンロードしたフォルダにjarファイルが入っていませんでした。
ソースコード
platform_packages_apps_settings-master/src/com/android/settings/wifi/p2p/WifiP2pPeer.java
Java
1 * Copyright (C) 2011 The Android Open Source Project 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15package com.android.settings.wifi.p2p; 16import com.android.settings.R; 17import android.content.Context; 18import android.net.wifi.WifiManager; 19import android.net.wifi.p2p.WifiP2pDevice; 20import android.preference.Preference; 21import android.text.TextUtils; 22import android.view.View; 23import android.widget.ImageView; 24public class WifiP2pPeer extends Preference { 25 private static final int[] STATE_SECURED = {R.attr.state_encrypted}; 26 public WifiP2pDevice device; 27 private final int mRssi; 28 private ImageView mSignal; 29 private static final int SIGNAL_LEVELS = 4; 30 public WifiP2pPeer(Context context, WifiP2pDevice dev) { 31 super(context); 32 device = dev; 33 setWidgetLayoutResource(R.layout.preference_widget_wifi_signal); 34 mRssi = 60; //TODO: fix 35 } 36 @Override 37 protected void onBindView(View view) { 38 if (TextUtils.isEmpty(device.deviceName)) { 39 setTitle(device.deviceAddress); 40 } else { 41 setTitle(device.deviceName); 42 } 43 mSignal = (ImageView) view.findViewById(R.id.signal); 44 if (mRssi == Integer.MAX_VALUE) { 45 mSignal.setImageDrawable(null); 46 } else { 47 mSignal.setImageResource(R.drawable.wifi_signal_dark); 48 mSignal.setImageState(STATE_SECURED, true); 49 } 50 refresh(); 51 super.onBindView(view); 52 } 53 @Override 54 public int compareTo(Preference preference) { 55 if (!(preference instanceof WifiP2pPeer)) { 56 return 1; 57 } 58 WifiP2pPeer other = (WifiP2pPeer) preference; 59 // devices go in the order of the status 60 if (device.status != other.device.status) { 61 return device.status < other.device.status ? -1 : 1; 62 } 63 // Sort by name/address 64 if (device.deviceName != null) { 65 return device.deviceName.compareToIgnoreCase(other.device.deviceName); 66 } 67 return device.deviceAddress.compareToIgnoreCase(other.device.deviceAddress); 68 } 69 int getLevel() { 70 if (mRssi == Integer.MAX_VALUE) { 71 return -1; 72 } 73 return WifiManager.calculateSignalLevel(mRssi, SIGNAL_LEVELS); 74 } 75 private void refresh() { 76 if (mSignal == null) { 77 return; 78 } 79 Context context = getContext(); 80 mSignal.setImageLevel(getLevel()); 81 String[] statusArray = context.getResources().getStringArray(R.array.wifi_p2p_status); 82 setSummary(statusArray[device.status]); 83 } 84}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。